bbgil
Full Member level 2
Hi. guys. got some things here that you might be able to help me out. I am able to get adc going by measuring a voltage divider, able to send it to lcd and get it to the gui i created in c#. Here is the problem. as the adc is updating, so is the gui keeps getting data but its continiuous, example is voltage reading = 3.7. i am getting in gui, 373737373737. Also how to put the dot (.) between the numbers? any suggestion is welcome. I have the code here.
Code:
#include <pic.h> // standard pic header file
#include <stdio.h>
#include <htc.h>
#include <conio.h>
#include "usart.h"//Adapted from http://electronics-diy.com
#include <string.h> // string function header file
#include "delay.h"
#include "delay.c"
#include "lcd.h"
#include "lcd.c"//Rs-RD4, EN-RD5, RD0-RD3 -- pin 11 -14 of LCD
// Configuration Fuses
#if defined(_16F877A)
#warning PIC16F877A Selected
__CONFIG(0x3F61);
#else
#error Unsupported PICmicro MCU Selected
#endif
// Global Variables
#define LED1 RB6 //LED connects to port B
#define LED2 RB7 //LED connects to port B
#define LEDS_8 PORTB
#define LEDS_2 PORTC
void get_adc ();
void get_adc()
{
//8bit data-type variables
unsigned char x; //ADC low byte
unsigned char y; //ADC high byte
unsigned long press,calc,w, z;
unsigned long inter, deci;
DelayMs (1);
//Start a2d conversion
//Set GO bit (ADCON0=ADCON0|1;)
GODONE=1;
//wait end-of-conversion (conversion complete)
while(GODONE==1);
x=ADRESL; //store low byte
y=ADRESH; //store high byte
w=(256*y+x); //multiply the signal together
//*256.0 is to get the overall stage
press = w*500/1023 ;
inter = (press/100)%10;
deci = (press/10)%10 ;
lcd_goto (0x0b);
lcd_putch (inter | 0x30 );
lcd_goto (0x0c); // move cursor to Row 0 Colum 11
lcd_puts("."); //display “.” (Decimal point)
lcd_goto (0x0D);
lcd_putch ( deci| 0x30);
lcd_goto (0x0F);
lcd_putch ( 0x56 );// letter V for voltage
if (RCIF) {
z = (inter |0x30);
TXREG = z;
TXREG = (0x2E);
calc = (deci|0x30);
TXREG = calc;
DelayMs (100);
RCIF =0;
TXIF = 0;
}
}
/
// Send Message Through USART
void SendMessage(const char Message[ ])
{
unsigned char MessageOffset = 0;
unsigned char temp;
while ((temp = Message[MessageOffset]) != '\0') // While not end of string
{ //
if (TXIF) // Ready to transmit next character?
{ //
TXIF = 0; // Clear transmitted character empty flag
TXREG = temp; // Send Character to transmittion register
MessageOffset++; // increment Character Index
}
}
}
// main program
void main()
{
// Constant Strings
const char Greetings[ ] = "\r\nHi, you are now connected !!\r\n";
const char NewLine[ ] = "\r\n"; // New Line
char temp; // Temporary Storage
const char Instruction[ ] = "\r\nREADING.... ";
const char Voltage[ ] = "\r\nVoltage is :\r\n";
unsigned long press, deci, inter;
init_comms();// Sets up the USART - settings defined in usart.h
TRISB = 0;; // Initialize LED tristate
PORTB = 0; // LED nitially off
// ADFM=1, all i/ps analog, +VREF enabled
//Configure the functions of the Port bits
ADCON1=0b10000001;
//clock/channel select & enable bits
//controls the operation of the A/D module
ADCON0=0b11000001;
//TRISC=0b00111111;
TRISA=0xFF; //inputs
LEDS_8=0xFF; //clear/switch off LEDS
LEDS_2=0xFF;
lcd_init();
lcd_puts ( "READING" );
DelayMs (100);
lcd_clear();
lcd_puts ( "Voltage = " );
SendMessage(Greetings); // Send Greeting Message
SendMessage(Instruction);
SendMessage(Voltage);
while (1==1)
{
get_adc();
}
}