Hello! I'm new in PIC programming in C. I was given an FM Radio assignment that required me to write a program to increase and decrease the digits when push buttons are pressed. The microcontroller I'm using PIC18F6490. Below is the part of the program that I have created. I warn you, it is lengthy!
Initialization
void Init() {
int i;
// buttons = 0;
OSCCON = 0b01110010; // Select 8 MHz internal oscillator
LCDSE0 = 0b11111111; // Enable LCD segments 07-00
LCDSE1 = 0b11111111; // Enable LCD segments 15-08
LCDSE2 = 0b11111111; // Enable LCD segments 23-16
LCDSE3 = 0b00000000; // Disable LCD segments 31-24
LCDCON = 0b10001000; // Enab LC controller. Static mode. INTRC clock
LCDPS = 0b00110110; // 37 Hz frame frequency
ADCON1 = 0b00111111; // Make all ADC/IO pins digital
TRISA = 0b00000011; // RA0 and RA1 pbutton
TRISB = 0b00100001; // RB0 and RB5 pbutton
TRISC = 0b00011000; // RC3 and RC4 do the I2C bus
TRISG = 0b11111111; // RG0, RG1 & RG3 pbutton
PORTA = 0;
PORTB = 0;
PORTC = 0;
INTCONbits.TMR0IF = 0; // Clear timer flag
T0CON = 0b00000011; // Prescale by 16
T0CON = 0b00001000; // No prescale
TMR0H = 0; // Clear timer count
TMR0L = 0;
T0CONbits.TMR0ON = 1; // Start timer
OpenI2C( MASTER, SLEW_OFF);
SSPADD = 0x3F;
// vi = 8;
}
Assigning bits to LCD segments
void digwrt(int num1, int num2, int num3)
{
switch (num1){
case 0: LCDDATA0 = 0b00111111;
LCDDATA2 = 0b01000000;break;
case 1: LCDDATA0 = 0b00000110; LCDDATA2 = 0b01000000;break;
case 2: LCDDATA0 = 0b01011011; LCDDATA2 = 0b01000000;break;
case 3: LCDDATA0 = 0b01001111; LCDDATA2 = 0b01000000;break;
case 4: LCDDATA0 = 0b01100110; LCDDATA2 = 0b01000000;break;
case 5: LCDDATA0 = 0b01101101; LCDDATA2 = 0b01000000;break;
case 6: LCDDATA0 = 0b01111101; LCDDATA2 = 0b01000000;break;
case 7: LCDDATA0 = 0b00000111; LCDDATA2 = 0b01000000;break;
case 8: LCDDATA0 = 0b01111111; LCDDATA2 = 0b01000000;break;
case 9: LCDDATA0 = 0b01101111; LCDDATA2 = 0b01000000;break;
}
switch (num2){
case 0: LCDDATA1 = 0b00011111;
` LCDDATA0 = LCDDATA0 ^= 0b10000000;
LCDDATA2 = 0b01000000; break;
case 1: LCDDATA1 = 0b00000011; LCDDATA2 = 0b01000000;break;
case 2: LCDDATA1 = 0b00101101;
LCDDATA0 = LCDDATA0 ^= 0b10000000; LCDDATA2 = 0b01000000;break;
case 3: LCDDATA1 = 0b00100111;
LCDDATA0 = LCDDATA0 ^= 0b10000000; LCDDATA2 = 0b01000000;break;
case 4: LCDDATA1 = 0b00110011;
LCDDATA2 = 0b01000000;break;
case 5: LCDDATA1 = 0b00110110;
LCDDATA0 = LCDDATA0 ^= 0b10000000; LCDDATA2 = 0b01000000;break;
case 6: LCDDATA1 = 0b00111110;
LCDDATA0 = LCDDATA0 ^= 0b10000000; LCDDATA2 = 0b01000000;break;
case 7: LCDDATA1 = 0b00000011;
LCDDATA0 = LCDDATA0 ^= 0b10000000; LCDDATA2 = 0b01000000;break;
case 8: LCDDATA1 = 0b00111111;
LCDDATA0 = LCDDATA0 ^= 0b10000000; LCDDATA2 = 0b01000000;break;
case 9: LCDDATA1 = 0b00110111;
LCDDATA0 = LCDDATA0 ^= 0b10000000; LCDDATA2 = 0b01000000;break;
}
switch (num3){
case 0: LCDDATA0 = LCDDATA0 ^= 0b10000000;
LCDDATA2 = 0b01001111;
LCDDATA1 = LCDDATA1 ^= 0b11000000; break;
case 1: LCDDATA2 = 0b01000001;
LCDDATA1 = LCDDATA1 ^= 0b10000000; break;
case 2:
LCDDATA2 = 0b01010110;
LCDDATA1 = LCDDATA1 ^= 0b11000000; break;
case 3:
LCDDATA2 = 0b01010011;
LCDDATA1 = LCDDATA1 ^= 0b11000000; break;
case 4: LCDDATA2 = 0b01011001;
LCDDATA1 = LCDDATA1 ^= 0b10000000; break;
case 5: LCDDATA2 = 0b01011011;
LCDDATA1 = LCDDATA1 ^= 0b01000000; break;
case 6: LCDDATA2 = 0b01011111;
LCDDATA1 = LCDDATA1 ^= 0b01000000; break;
case 7: LCDDATA2 = 0b01000001;
LCDDATA1 = LCDDATA1 ^= 0b11000000; break;
case 8: LCDDATA2 = 0b01011111;
LCDDATA1 = LCDDATA1 ^= 0b11000000; break;
case 9: LCDDATA2 = 0b01011011;
LCDDATA1 = LCDDATA1 ^= 0b11000000; break;
case 10:
LCDDATA2 = 0b01101111;
LCDDATA1 = LCDDATA1 ^= 0b11000000; break;
}
}
Increment & Decrement of Digits (By pressing push button RA0 and RA1 respectively)
void main(void) {
unsigned char in=0,m=0,n=0;
int c, gerr, x, y, z;
int f;
dly(20);
Init();
FMinit();
x=5;
y=8;
z=8;
digwrt(x,y,z);
for (;;) {
dly(200);
if (!PORTAbits.RA0){
x++;
if((x==1)&&(y==8)&&(z==10)){
x=5;
y=7;
z=8;
digwrt(x,y,z);
}
else if(x==10){
x=0;
y++;
if(y==10){
y=0;
z++;
if(z==10){
z=10;
}
}
digwrt(x,y,z);
}
else{
digwrt(x,y,z);
dly(100);
}
f = (x+(10*y)+(100*z))+690;
FMfrequenc(f);
}
else if(!PORTAbits.RA1){
x--;
if((x==4)&&(y==7)&&(z==8)){
x=0;
y=8;
z=10;
digwrt(0,8,10);
}
else if(x==-1){
x=9;
y--;
if(y==-1){
y=9;
z--;
}
digwrt(x,y,z);
}
else{
digwrt(x,y,z);
dly(100);
}
f = (x+(10*y)+(100*z))+690;
FMfrequenc(f);
}
This third part is where the problem is located. When power is supplied to the LCD, the program starts counting automatically from 88.5 to 108.1 automatically with continuous looping and there is no effect when we pushed the RA0/RA1 button. There is no hardware issues with the pushbutton (the voltage goes from Vdd to 0v when pressed), so I am quite confused.
Any help is very much appreciated I could also supply the whole code if it is necessary to help you guys analyze the program.