geotech
Newbie level 1
- Joined
- May 18, 2013
- Messages
- 1
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,293
Hi everyone, my name is James, this is my first post on this forum. I am here because i have been driven nuts
trying to find why my pic project has stopped working! I am trying to implement a simple menu on an LCD, 3 push buttons, PIC16F1507, Hi-Tech C, MPLAB v8.84. I have declared an array of structs, each element holds a struct that contains a text string and an int, each time a menu button is pressed it displays the text string and int value of the next element in the array, looping back to the begining of the array when it reaches the end. There is also a plus and minus buttons that allow the int value in the selected struct to be incremented or decremented.
This worked fine for a while, but after i changed some other unrelated code, it has ceased to display the contents of the struct. I cannot for the life of me figure out why. I have wondered if there may be an issue with the struct spanning across memory page boundries? BTW I have RTFM and STFW but it seems on this occasion that Google is not my friend
Here's the key parts of my code, if anyone can spot the problem, i will be so very grateful.
MAIN.C
LCD.C
Kind Regards, J.
trying to find why my pic project has stopped working! I am trying to implement a simple menu on an LCD, 3 push buttons, PIC16F1507, Hi-Tech C, MPLAB v8.84. I have declared an array of structs, each element holds a struct that contains a text string and an int, each time a menu button is pressed it displays the text string and int value of the next element in the array, looping back to the begining of the array when it reaches the end. There is also a plus and minus buttons that allow the int value in the selected struct to be incremented or decremented.
This worked fine for a while, but after i changed some other unrelated code, it has ceased to display the contents of the struct. I cannot for the life of me figure out why. I have wondered if there may be an issue with the struct spanning across memory page boundries? BTW I have RTFM and STFW but it seems on this occasion that Google is not my friend
Here's the key parts of my code, if anyone can spot the problem, i will be so very grateful.
MAIN.C
Code:
//****************************Global Variables**********************//
struct aMenu {
unsigned char label[11];
unsigned int val;
}menu[3] = {{"Speed \0", 25}, {"Threshold\0", 5}, {"unused \0", 20}};
void main()
{
/*********************** Button Event Handlers ********************/
if(ButtonEvent && B1_flag) //////////////////////// Down Handler
{
menu[MenuIndex].val--;
lcd_goto(LINE2+12);
LCDWriteInt(menu[MenuIndex].val, 3);
ButtonEvent = FALSE;
}
if(ButtonEvent && B2_flag) /////////////////////// Up Handler
{
menu[MenuIndex].val++;
lcd_goto(LINE2+12);
LCDWriteInt(menu[MenuIndex].val, 3);
ButtonEvent = FALSE;
}
if(ButtonEvent && B3_flag) /////////////////////// Menu Handler
{
MenuIndex++;
if(MenuIndex > 2) MenuIndex = 0;
lcd_clear();
lcd_goto(LINE1);
lcd_puts("Adjustment Menu");
lcd_goto(LINE2);
lcd_puts(menu[MenuIndex].label);
lcd_goto(LINE2+12);
LCDWriteInt(menu[MenuIndex].val, 3);
ButtonEvent = FALSE;
}
}// end main
LCD.C
Code:
/* write a byte to the LCD in 4 bit mode */
void
lcd_write(unsigned char c)
{
if(c & 0x80) LCD_D7=1; else LCD_D7=0;
if(c & 0x40) LCD_D6=1; else LCD_D6=0;
if(c & 0x20) LCD_D5=1; else LCD_D5=0;
if(c & 0x10) LCD_D4=1; else LCD_D4=0;
LCD_STROBE;
if(c & 0x08) LCD_D7=1; else LCD_D7=0;
if(c & 0x04) LCD_D6=1; else LCD_D6=0;
if(c & 0x02) LCD_D5=1; else LCD_D5=0;
if(c & 0x01) LCD_D4=1; else LCD_D4=0;
LCD_STROBE;
__delay_us(40);
}
/* 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(unsigned 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(pos);
}
/* initialise the LCD - put into 4 bit mode */
void
lcd_init(void)
{
LCD_RS = 0; // write control bytes
__delay_ms(15);// power on delay
LCD_D4 = 1; // init!
LCD_D5 = 1; //
LCD_STROBE;
__delay_ms(5);
LCD_STROBE; // init!
__delay_us(100);
LCD_STROBE; // init!
__delay_ms(5);
LCD_D4 = 0; // set 4 bit mode
LCD_STROBE;
__delay_us(40);
lcd_write(0x28);// 4 bit mode, 1/16 duty, 5x8 font, 2lines
lcd_write(0x0C);// display on
lcd_write(0x06);// entry mode advance cursor
lcd_write(0x01);// clear display and reset cursor
}
void LCDWriteInt(int val,unsigned int field_length)
{
/***************************************************************
This function writes a integer type value to LCD module
Arguments:
1)int val : Value to print
2)unsigned int field_length :total length of field in which the value is printed
must be between 1-5 if it is -1 the field length is no of digits in the val
****************************************************************/
LCD_RS = 1; // write characters
char str[5]={0,0,0,0,0};
int i=4,j=0;
while(val)
{
str[i]=val%10;
val=val/10;
i--;
}
if(field_length==-1)
while(str[j]==0) j++;
else
j=5-field_length;
if(val<0) lcd_write('-');
for(i=j;i<5;i++)
{
lcd_write(48+str[i]);
}
}
Kind Regards, J.