Microcontroller › PIC › How to get addc value through uart
- This topic has 1 reply, 2 voices, and was last updated 8 years, 7 months ago by
Ashutosh Bhatt.
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
July 8, 2016 at 5:42 am #4509
NANDHAkumar
ParticipantI got adc value on LCD but i cannot get values through UART
Hitech c compiler
MPLAB IDE
#include "Includes.h"// Configuration word for PIC16F877__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON& LVP_OFF & CPD_OFF & WRT_ON & DEBUG_OFF);#define AN7 7#define BAUDRATE 9600 //bps//unsigned char ch="ADC DATA ACQUISTION";unsigned int result [11];void InitADC(void){ADCON1 = 0x80; // Make PORTA and PORTE analog pins// Also, Vref+ = 5v and Vref- = GNDTRISA = 0x2f; // Make RA5, RA3, RA2, RA1, RA0 inputTRISE = 0x07; // Make RE0, RE1 and RE2 inputADCON0 = 0x81; // Turn on the A/D Converter}void InitUART(void){TRISC6 = 1; // TX PinTRISC7 = 1; // RX PinSPBRG = ((_XTAL_FREQ/16)/BAUDRATE) – 1;BRGH = 1; // Fast baudrateSYNC = 0; // AsynchronousSPEN = 1; // Enable serial port pinsCREN = 1; // Enable receptionSREN = 0; // No effectTXIE = 0; // Disable tx interruptsRCIE = 1; // Enable rx interruptsTX9 = 0; // 8-bit transmissionRX9 = 0; // 8-bit receptionTXEN = 0; // Reset transmitterTXEN = 1; // Enable the transmitter}unsigned int GetADCValue(unsigned char Channel){ADCON0 &= 0xc7; // Clear Channel selection bitsunsigned char temp = 0;//unsigned int num = 0;unsigned int num = 0;ADON=1;ADCON0|=0b01000000;while(ADCON0==0x11) // wait for the A/D conversion to finishresult [10]= ADRESL & 0xff; // store the lower eight bits in their respective place in the results arraytemp = ADRESH & 0x03; // get the uppermost two bits}void SendByteSerially(unsigned char byte) // Writes a character to the serial port{while(!TXIF); // wait for previous transmission to finishTXREG = result [10];while(!TXIF);}// Main Functionvoid main(void){unsigned int ADC_value = 0;unsigned int digit1, digit2, digit3, digit4;InitADC(); // Initialize ADCInitLCD(); // Initialize LCDInitUART();//void SendByteSerially(unsigned char);while(1){ClearLCDScreen(); // Clear LCD screenGetADCValue(AN7); // Read ADC value from RE2(AN7) pin__delay_ms(1000);void SendByteSerially(unsigned char byte);// ADC_value can have a value from 0 (0v) to 1023(5v) only.// SO display 4 digits of ADC_valuedigit1 = (unsigned int)(ADC_value/1000); // Calculate digit1 of ADC_valuedigit2 = (unsigned int)((ADC_value – digit1*1000)/100); // Calculate digit2 of ADC_valuedigit3 = (unsigned int)((ADC_value – (digit1*1000+digit2*100))/10); // Calculate digit3 of ADC_valuedigit4 = (unsigned int)(ADC_value – (digit1*1000+digit2*100+digit3*10)); // Calculate digit4 of ADC_valueWriteDataToLCD(digit1+0x30); // Display digit1 of ADC_value on LCDWriteDataToLCD(digit2+0x30); // Display digit2 of ADC_value on LCDWriteDataToLCD(digit3+0x30); // Display digit3 of ADC_value on LCDWriteDataToLCD(digit4+0x30); // Display digit4 of ADC_value on LCD__delay_ms(1000); // Half second delay before next reading}}July 9, 2016 at 7:59 pm #14054Ashutosh Bhatt
Participantmay be the problem in baud rate. just check crystal frequency and com port setting
-
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.