by teochiashen » Mon Apr 23, 2012 10:20 am
I try to write the code for microcontroller so that it will display the temperature and water pump on my LCD display, when I compile it after I wrote it on MikroC is no problem, but when I program into my microcontroller, it shows no display at all. I don't know what's the problem? ANd I try to modify the code refer to the book (PIC Microcontrollers: Programming in C). Below is my programming code. The display should be in two lines like this. [(first line) temp:__.__'C (second line) water pump:___] I think maybe is the problem with the ADC conversion. Please help me.
LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB05_bit;
// End LCD module connections
// Initialize variables
unsigned char ch1, ch2;
unsigned int adc_rd_temp, adc_rd_volume;
char txt1[] = "temp:";
char txt2[] = "water pump:";
const temp = 25;
long tlong_temp, tlong_volume;
// Main routine
void main() {
ADC_Init();
INTCON = 0; // All interrupts disabled
ADCON1 = 1; // Set all port A pins as ADC
TRISA = 0xFF; // All port A pins are configured as inputs
TRISB = 0; // All port B pins are configured as outputs
TRISC = 0xF9; // Port C pins RC1 and RC2 are configured as outputs
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // LCD command (cursor off)
Lcd_Cmd(_LCD_CLEAR); // LCD command (clear LCD)
Lcd_Out(1,1,txt1);
Lcd_Chr(1,8,223); // Print degree character, 'C' for Centigrades
Lcd_Chr(1,9,'C');
Lcd_Out(2,1,txt2);
delay_ms(1000);
while(1) {
adc_rd_temp = ADC_Read(0); // A/D conversion. Pin RA0 is an input
delay_ms(1000);
tlong_temp = (long)adc_rd_temp * 5000; // The input multiply with ADC resolution. Convert the result in milivolts
tlong_temp = tlong_temp / 1023; // 0..1023 -> 0-5000mV
tlong_temp = tlong_temp * 100;
ch1 = tlong_temp / 1000; // Extract volts (thousands of millivolts)
// from result
Lcd_Chr(1,6,48+ch1); // Write result in ASCII format
Lcd_Chr_CP('.');
ch1 = (tlong_temp / 100) % 10; // Extract hundreds of millivolts
Lcd_Chr_CP(48+ch1); // Write result in ASCII format
ch1 = (tlong_temp / 10) % 10; // Extract tens of millivolts
Lcd_Chr_CP(48+ch1); // Write result in ASCII format
delay_ms(1);
adc_rd_volume = ADC_Read(2); // A/D conversion. Pin RA1 is an input
delay_ms(1000);
tlong_volume = ((long)adc_rd_volume*5)/1023; // The input multiply with ADC resolution.
ch2 = tlong_volume;