Uart xc8 a little help formatting

Status
Not open for further replies.

be80be

Well-Known Member
Ok I got some code working it sends back what I send but I want under stand how to use this to send out adc value.
Here the main code
Code:
while(1)
  {
     a = ADC_Read(0); //Reading Analog Channel 0
          USARTWrite(a);
          __delay_ms(1000); // 1 Second Delay
  }
}

Here the function
Code:
void USARTWrite(char data)
{
  while(!TRMT);
  TXREG = data;
}
This is what is showing up
Code:
ca fb da da 00 fb db 58 de da
If i hold the adc to ground i get this
Code:
00 00 00 00 00 00 00 00 00
so it sending
 
Last edited:
You are trying to send a 10 bit value as a byte. You need to split it up and have someway to identify the high byte. You could send it as a 7 bit and a 3 bit value with the 3 bit value always having the MSB set to indicate it is the MSB.

Mike.
Edit,
USARTWrite(a & 127);
USARTWrite((a>>7) | 128);
 
In the first post the PC appears to be displaying the hex value but in the second it's trying to display the ascii character. You should get values like 37 82 for the hex value 137 and 00 80 for zero.

Mike.
 
I'd like to get the output to show the number 1023 lol

This is how this works
USARTWrite(a & 127); this at ground sends 00
USARTWrite((a>>7) | 128); this if at ground sends 80

With both in main I get 00 80 00 80 so on
hoe do i get a plain 0 to 1023 to show up
 
Last edited:
Then you can either send it as above and reassemble it on the PC side or convert to ascii.
Code:
char str[6];
itoa(a,str,10);
i=0;
while(str[i])
     USARTWrite(str[i++]);
USARTWrite(',');

Mike.
Edit, you will have to include stdlib.h
 
Last edited:
Mplab-X crashed re start
got this now
 
This fixed all but one error the itoa it cant' find it
Code:
 while(1)
  {
     a = ADC_Read(0); //Reading Analog Channel 0
             char str[6];
     itoa(a,str,10);
     int i=0;
    while(str[i])
      USARTWrite(str[i++]);
      USARTWrite(',');
          __delay_ms(1000); // 1 Second Delay
  }
}
I figured the itoa
But I now have no output at all
 
Last edited:
I did but i now don't have any output to serial
The Mplab crash didn't save my file where I added #include <stdlib.h> And the code you had posted Mike

This it what I'm using now
Code:
// Configuration Byte
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
#include <xc.h>
#include <stdio.h>              // Standard I/O - required for printf() function
#include <stdlib.h>              // formatting text
#include "usart_pic16.h"
#define _XTAL_FREQ  20000000
void ADC_Init()
{
  ADCON0 = 0x41; //ADC Module Turned ON and Clock is selected
  ADCON1 = 0B10001111; //All pins as Analog Input
                 //With reference voltages VDD and VSS
}
unsigned int ADC_Read(unsigned char channel)
{
  if(channel > 7) //If Invalid channel selected
    return 0;     //Return 0
  ADCON0 &= 0xC5; //Clearing the Channel Selection Bits
  ADCON0 |= channel<<3; //Setting the required Bits
  __delay_ms(2); //Acquisition time to charge hold capacitor
  GO_nDONE = 1; //Initializes A/D Conversion
  while(GO_nDONE); //Wait for A/D Conversion to complete
  return ((ADRESH<<8)+ADRESL); //Returns Result
}
void main()
{
  //Initialize USART with baud rate 9600
  USARTInit(9600);
  TRISA = 0B00000001;
  ADC_Init();
  int a;
  while(1)
  {
     a = ADC_Read(0); //Reading Analog Channel 0
             char str[6];
     itoa(a,str,10);
     int i=0;
    while(str[i])
      USARTWrite(str[i++]);
      USARTWrite(',');
          __delay_ms(1000); // 1 Second Delay
  }
}
The zip is the whole ball of wax

Thanks Mike for helping this driving me crazy maybe a change chip the 16f18xxx chips have 16 it buffer.
 

Attachments

  • USARTTest.zip
    131.9 KB · Views: 284
Last edited:
There is a function that directs the output to the output of choice But you must write it!
C:
void  putch(char c)
   {
   USARTWrite(c);
   }
write this above your main.. Then you can just do this..

printf("%d",ADC_Read(0));
 
I get this
usart_test.c:52: error: (314) ";" expected
usart_test.c:55: error: (194) ")" expected
usart_test.c:58: error: (285) no identifier in declaration
usart_test.c:58: warning: (374) missing basic type; int assumed
usart_test.c:58: error: (314) ";" expected
(908) exit status = 1
nbproject/Makefile-default.mk:130: recipe for target 'build/default/production/usart_test.p1' failed
make[2]: *** [build/default/production/usart_test.p1] Error 1
Code:
void main()
{
  //Initialize USART with baud rate 9600
  USARTInit(9600);
  TRISA = 0B00000001;
  ADC_Init();
  int c = 0;
  void  putch(char c)
   {
   USARTWrite(c);
   }
  printf("%d",ADC_Read(0));
       //__delay_ms(1000); // 1 Second Delay 
 // }
}
 
Above the main... Not in it!!!
Code:
void  putch(char c)
   {
   USARTWrite(c);
   }
void main()
{
  //Initialize USART with baud rate 9600
  USARTInit(9600);
  TRISA = 0B00000001;
  ADC_Init();
  int c = 0;
  printf("%d",ADC_Read(0));
       //__delay_ms(1000); // 1 Second Delay
 // }
}
 
That works now it figure new line
printf("%d\n",100);
That works
Bad news is I killed my adc i moved to bigger breadboard and hook the power up and set my USB to serial on fire LOL now no adc to LOL
 
Last edited:
I changed chips I now only get to reading 0 and 1023 there is nothing showing if I move the pot

no range there

I have the pot set to 2.3 volts its shows 1023
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…