Hey all,
I am a newb when it comes to microprocessors and the C programming language. I am currently working on a project and have hit a programming standstill. I am using C to perform a calculation to a variable declared as an integer. I currently am trying to output that integer to LCD using 8 bit transfer over PORTB of my chip. I believe I have to convert the integer to BCD, then convert to ASCII before outputting. Currently, I have the math done, then separated the resulting integer to a high byte and low byte char's. I will paste my existing code in below. Any help/suggestions would be greatly appreciated.
I am a newb when it comes to microprocessors and the C programming language. I am currently working on a project and have hit a programming standstill. I am using C to perform a calculation to a variable declared as an integer. I currently am trying to output that integer to LCD using 8 bit transfer over PORTB of my chip. I believe I have to convert the integer to BCD, then convert to ASCII before outputting. Currently, I have the math done, then separated the resulting integer to a high byte and low byte char's. I will paste my existing code in below. Any help/suggestions would be greatly appreciated.
Code:
#include <htc.h> //uses non-legacy pic16f877a.h
#include <math.h>
__CONFIG (0X02F0A);
#define SNSR1 RA2 //Sensor1
#define SNSR2 RA3 //Sensor2
#define LCD_EN RA0 //LCD register select
#define LCD_RS RA1 //LCD enable
#define LCD_DATA PORTB //LCD 8-bit parallel data
int vol @ 0X40;
unsigned char count @ 0X44;
unsigned char hi_byte @ 0X4A;
unsigned char lo_byte @ 0X4B;
/************************************************************/
/* Apply a pulse to the LCD Enable (E) input */
/************************************************************/
void lcd_toggle()
{
LCD_EN=1;
_delay(5); //1us delay
LCD_EN=0;
}
/************************************************************/
/* Write one byte to LCD w/no change to RS line */
/************************************************************/
void lcd_write(unsigned char c)
{
_delay(200); //40us delay
LCD_DATA = (c);
lcd_toggle(); //put data (character) into LCD
}
/************************************************************/
/* Clear the LCD and go to home position */
/************************************************************/
void lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x01);
_delay(10000); //2ms delay
}
/************************************************************/
/* Set RS line hi and write string of characters to LCD */
/************************************************************/
void lcd_charstring(const char * s)
{
LCD_RS = 1; //write data (characters)
while(*s)
lcd_write(*s++);
}
/************************************************************/
/* Set RS line hi and write a character to LCD */
/************************************************************/
void lcd_char(char c)
{
LCD_RS = 1;
lcd_write( c );
}
/************************************************************/
/* Set LCD cursor to a specific location LCD */
/************************************************************/
void lcd_goto(unsigned char pos)
{
LCD_RS = 0; //write instruction
lcd_write(0x80+pos);
}
/************************************************************/
/* Initialize LCD - 0x30,0x30,0x30,0x38,0x0C,0x01,0x06,0x02 */
/************************************************************/
void lcd_init() //initialize to 8 bit mode
{
char init_value;
init_value = 0x30;
ADCON1=0b00000111;
TRISA=0b01100; //PORTA all outputs (E and RS)
TRISB=0b00000000; //PORTB all outputs (data lines)
LCD_RS = 0;
LCD_EN = 0;
_delay(75000); //wait 15ms after power applied,
LCD_DATA = init_value; //execute 3 times
lcd_toggle(); //put instruction into LCD
_delay(25000); //5ms delay
lcd_toggle();
_delay(1000); //200us delay
lcd_toggle();
_delay(1000); //200us delay
LCD_DATA = 0x38; //8-bit mode; 2 lines; 5x8 dots
lcd_toggle();
lcd_write(0x0C); //display on, cursor off, position 0
lcd_clear(); //clear screen
lcd_write(0x06); //set cursor mode to increment
}
/************************************************************/
/* Intro Message */
/************************************************************/
void lcd_intro()
{
lcd_init();
_delay(150000);
lcd_goto(0);
lcd_charstring("Infrared Ballistic Chronograph");
_delay(2500000);
lcd_clear();
}
/************************************************************/
/* Velocity Calculation */
/************************************************************/
void calc()
{
hi_byte=0x00;
lo_byte=0x00;
vol=(50000/count);
hi_byte=(vol >> 8);
lo_byte=vol;
//THIS IS WHERE I'M STUCK
}
/************************************************************/
/* Main Program */
/************************************************************/
void main(void)
{
count=0x64;
lcd_intro();
while(SNSR1 == 0 || SNSR1 == 1)
{
if (SNSR1 == 1)
{
while(SNSR2 == 0)
{
_delay(100); //20us delay
count++;
}
lcd_init();
_delay(150000);
lcd_goto(0);
calc();
for(;; );
}
}
}
Last edited by a moderator: