• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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:

Pommie

Well-Known Member
Most Helpful Member
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);
 

be80be

Well-Known Member
Im getting this now
Screenshot from 2018-01-20 06-49-27.png

See it sends text great
Screenshot from 2018-01-20 06-53-32.png
 
Last edited:

Pommie

Well-Known Member
Most Helpful Member
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.
 

be80be

Well-Known Member
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:

Pommie

Well-Known Member
Most Helpful Member
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:

be80be

Well-Known Member
Mplab-X crashed re start
got this now
/opt/microchip/xc8/v1.44/bin/xc8" --pass1 --chip=16F876 -Q -G --double=24 --float=24 --opt=+asm,-asmfile,+speed,-space,-debug,-local --addrqual=ignore --mode=free -D_XTAL_FREQ=20000000 -P -N255 --warn=0 --asmlist -DXPRJ_default=default --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-osccal,-resetbits,-download,-stackcall,+clib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s" -obuild/default/production/usart_test.p1 usart_test.c
usart_test.c:54: warning: (361) function declared implicit int
usart_test.c:55: error: (192) undefined identifier "i"
(908) exit status = 1
nbproject/Makefile-default.mk:130: recipe for target 'build/default/production/usart_test.p1' failed
make[2]: Leaving directory '/home/burt/Desktop/USARTTest.X'
nbproject/Makefile-default.mk:90: recipe for target '.build-conf' failed
make[1]: Leaving directory '/home/burt/Desktop/USARTTest.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make[2]: *** [build/default/production/usart_test.p1] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 360ms)
 

be80be

Well-Known Member
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:

be80be

Well-Known Member
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

Last edited:

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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));
 

be80be

Well-Known Member
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 
 // }
}
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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
 // }
}
 

be80be

Well-Known Member
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:

be80be

Well-Known Member
I changed chips I now only get to reading 0 and 1023 there is nothing showing if I move the pot
Screenshot from 2018-01-20 10-45-20.png
no range there

I have the pot set to 2.3 volts its shows 1023
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top