haronraziq
Newbie level 6
- Joined
- Apr 3, 2011
- Messages
- 14
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 1,283
- Activity points
- 1,449
I am trying to program a microcontroller for temperature sensing and output to an lcd. I have programs both in Fed C and Hi-tech C included below.
The FED C program has voltages at the outputs but the LCD remains with a black lined display (all black boxes)
The Hi-tech C program does not have output voltage and the LCD remains black lined.
Can anyone see what is causing this problem in my programs? I would really appreciate the help.
#ifndef _XTAL_FREQ
// Unless specified elsewhere, 4MHz system frequency is assumed
#define _XTAL_FREQ 4000000
#endif
#include <htc.h>
#include "lcd.h"
#include <string.h>
#include <math.h>
#include <stdio.h>
#define LCD_RS RC3
#define LCD_RW RC2
#define LCD_EN RC1
#define LCD_DATA PORTD
#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
/* write a byte to the LCD in 4 bit mode */
void
lcd_write(unsigned char c)
{
__delay_us(40);
LCD_DATA = ( ( c >> 4 ) & 0x0F );
LCD_STROBE();
LCD_DATA = ( c & 0x0F );
LCD_STROBE();
}
/*
* Clear and home the LCD
*/
void
lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x1);
__delay_ms(2);
}
/* write a string of chars to the LCD */
void
lcd_puts(const char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}
/* write one character to the LCD */
void
lcd_putch(char c)
{
LCD_RS = 1; // write characters
lcd_write( c );
}
/*
* Go to the specified position
*/
void
lcd_goto(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0x80+pos);
}
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
char init_value;
//ADCON1 = 0x06; // Disable analog pins on PORTA//
init_value = 0x3;
TRISC=0;
TRISD=0;
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;
__delay_ms(15); // wait 15mSec after power applied,
LCD_DATA = init_value;
LCD_STROBE();
__delay_ms(5);
LCD_STROBE();
__delay_us(200);
LCD_STROBE();
__delay_us(200);
LCD_DATA = 2; // Four bit mode
LCD_STROBE();
lcd_write(0x28); // Set interface length
lcd_write(0xF); // Display On, Cursor On, Cursor Blink
lcd_clear(); // Clear screen
lcd_write(0x6); // Set entry Mode
}
void main()
{
float temp,LSB,lsd1;
int msd,lsd2;
unsigned char temperature[];
LSB = 5000.0/1024.0; //LSB in mV
TRISA = 1;
//Initialize the LCD//
lcd_init();
lcd_goto(0);
while(1) //DO FOREVER
{
/* Configure the A/D */
ADCON1 = 0x80; //Select 10 bits
ADCON0 = 0x41;
/* Start A/D conversion */
ADCON0=0x43; //Start A/D conversion
while((ADCON0 & 2) != 0); //Wait for conversion
temp=256.0*(float)ADRESH+(float)ADRESL; //Get temperature
/* Convert reading to temperature */
temp = temp*LSB; //temp in mV
temp = temp/10.0; //temp in degrees C
/* Format for the display */
//lcd_clear(); //Clear LCD and home//
msd=(int)temp; //msd digit
lsd1=10.0*(temp-msd); //lsd digit
lsd2=(int)lsd1;
sprintf(temperature,"%d",msd);
temperature[2]= '.';
sprintf(temperature+3,"%d",lsd2);
lcd_puts(temperature);
__delay_ms(1000);
}
}
The FED C program has voltages at the outputs but the LCD remains with a black lined display (all black boxes)
The Hi-tech C program does not have output voltage and the LCD remains black lined.
Can anyone see what is causing this problem in my programs? I would really appreciate the help.
#ifndef _XTAL_FREQ
// Unless specified elsewhere, 4MHz system frequency is assumed
#define _XTAL_FREQ 4000000
#endif
#include <htc.h>
#include "lcd.h"
#include <string.h>
#include <math.h>
#include <stdio.h>
#define LCD_RS RC3
#define LCD_RW RC2
#define LCD_EN RC1
#define LCD_DATA PORTD
#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
/* write a byte to the LCD in 4 bit mode */
void
lcd_write(unsigned char c)
{
__delay_us(40);
LCD_DATA = ( ( c >> 4 ) & 0x0F );
LCD_STROBE();
LCD_DATA = ( c & 0x0F );
LCD_STROBE();
}
/*
* Clear and home the LCD
*/
void
lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x1);
__delay_ms(2);
}
/* write a string of chars to the LCD */
void
lcd_puts(const char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}
/* write one character to the LCD */
void
lcd_putch(char c)
{
LCD_RS = 1; // write characters
lcd_write( c );
}
/*
* Go to the specified position
*/
void
lcd_goto(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0x80+pos);
}
/* initialise the LCD - put into 4 bit mode */
void
lcd_init()
{
char init_value;
//ADCON1 = 0x06; // Disable analog pins on PORTA//
init_value = 0x3;
TRISC=0;
TRISD=0;
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;
__delay_ms(15); // wait 15mSec after power applied,
LCD_DATA = init_value;
LCD_STROBE();
__delay_ms(5);
LCD_STROBE();
__delay_us(200);
LCD_STROBE();
__delay_us(200);
LCD_DATA = 2; // Four bit mode
LCD_STROBE();
lcd_write(0x28); // Set interface length
lcd_write(0xF); // Display On, Cursor On, Cursor Blink
lcd_clear(); // Clear screen
lcd_write(0x6); // Set entry Mode
}
void main()
{
float temp,LSB,lsd1;
int msd,lsd2;
unsigned char temperature[];
LSB = 5000.0/1024.0; //LSB in mV
TRISA = 1;
//Initialize the LCD//
lcd_init();
lcd_goto(0);
while(1) //DO FOREVER
{
/* Configure the A/D */
ADCON1 = 0x80; //Select 10 bits
ADCON0 = 0x41;
/* Start A/D conversion */
ADCON0=0x43; //Start A/D conversion
while((ADCON0 & 2) != 0); //Wait for conversion
temp=256.0*(float)ADRESH+(float)ADRESL; //Get temperature
/* Convert reading to temperature */
temp = temp*LSB; //temp in mV
temp = temp/10.0; //temp in degrees C
/* Format for the display */
//lcd_clear(); //Clear LCD and home//
msd=(int)temp; //msd digit
lsd1=10.0*(temp-msd); //lsd digit
lsd2=(int)lsd1;
sprintf(temperature,"%d",msd);
temperature[2]= '.';
sprintf(temperature+3,"%d",lsd2);
lcd_puts(temperature);
__delay_ms(1000);
}
}