Indramal
Newbie level 4
I code for reading EEPROM and edit those EEPROM data and work with those data. Normally I am coding something and upload to PIC and check that code.
When I am code more than some point it is not working. I connected LCD with PIC it also shows all pixels ON. when I remove codes in somewhere then again working. When codes are high PIC not working. When codes are less then PIC working.
But when I compiled it shows PIC have free ROM and RAM spaces.
I am using 4Mhz crystal oscillator and PIC18F4550 microcontroller. I am using MikroC for coding.
Why did this happen in Hardware?
My code:
When I am code more than some point it is not working. I connected LCD with PIC it also shows all pixels ON. when I remove codes in somewhere then again working. When codes are high PIC not working. When codes are less then PIC working.
But when I compiled it shows PIC have free ROM and RAM spaces.
I am using 4Mhz crystal oscillator and PIC18F4550 microcontroller. I am using MikroC for coding.
Why did this happen in Hardware?
My code:
Code:
// LCD module connections
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
unsigned short shedno=0,i=0,shedc=0,streg,lsreg,rang,x;
unsigned count=0,reg1, reg2, reg3, reg4, reg5, reg6,sttime=1000;
char shednon[4]="",countn[6]="",stregn[4]="",lsregn[4]="",regval[6]="",ntrel1[6]="",ntrel2[6]="",ntrel3[6]="",ntrel4[6]="",ntrel5[6]="",ntrel6[6]="";
void findshed(int sno){
shedno = EEPROM_Read(0x00);
shedno = 256 - shedno;
shedno = shedno + sno;
ByteToStr(shedno, shednon);
streg = 6*shedno+4;
lsreg = streg + 5;
ByteToStr(streg, stregn);
ByteToStr(lsreg, lsregn);
}
void shedrelay(){
rang =0;
for(x=streg; x<=lsreg; x++){
regval[rang] = EEPROM_Read(x);
Delay_ms(100);
if(rang < 6){
rang++;
}
}
reg1 = regval[0]+sttime;
reg2 = regval[1]+sttime;
reg3 = regval[2]+sttime;
reg4 = regval[3]+sttime;
reg5 = regval[4]+sttime;
reg6 = regval[5]+sttime;
Wordtostr(reg1,ntrel1);
Wordtostr(reg2,ntrel2);
Wordtostr(reg3,ntrel3);
Wordtostr(reg4,ntrel4);
Wordtostr(reg5,ntrel5);
Wordtostr(reg6,ntrel6);
}
void shedselectdis(){
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Next Shedule: S");
Lcd_Out(1,16,shednon);
Lcd_Out(2,1,"R1:");
Lcd_Out(2,4,ntrel1);
Lcd_Out(2,11,"R2:");
Lcd_Out(2,14,ntrel2);
Lcd_Out(3,1,"R3:");
Lcd_Out(3,4,ntrel3);
Lcd_Out(3,11,"R4:");
Lcd_Out(3,14,ntrel4);
Lcd_Out(4,1,"R5:");
Lcd_Out(4,4,ntrel5);
Lcd_Out(4,11,"R6:");
Lcd_Out(4,14,ntrel6);
}
void editshed(){
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"Edit Shedule: S");
Lcd_Out(1,16,shednon);
Lcd_Out(2,1,"R1:");
Lcd_Out(2,4,ntrel1);
Lcd_Out(2,11,"R2:");
Lcd_Out(2,14,ntrel2);
Lcd_Out(3,1,"R3:");
Lcd_Out(3,4,ntrel3);
Lcd_Out(3,11,"R4:");
Lcd_Out(3,14,ntrel4);
Lcd_Out(4,1,"R5:");
Lcd_Out(4,4,ntrel5);
Lcd_Out(4,11,"R6:");
Lcd_Out(4,14,ntrel6);
}
void relayssw(int x1, int x2, int x3, int x4, int x5, int x6){
PORTA = 0b00000001;
VDelay_ms(x1);
PORTA = 0b00000000;
Delay_ms(50);
PORTA = 0b00000010;
VDelay_ms(x2);
PORTA = 0b00000000;
Delay_ms(50);
PORTA = 0b00000100;
VDelay_ms(x3);
PORTA = 0b00000000;
Delay_ms(50);
PORTA = 0b00001000;
VDelay_ms(x4);
PORTA = 0b00000000;
Delay_ms(50);
PORTA = 0b00010000;
VDelay_ms(x5);
PORTA = 0b00000000;
Delay_ms(50);
PORTA = 0b00100000;
VDelay_ms(x6);
PORTA = 0b00000000;
Delay_ms(50);
}
void disval(){
Lcd_Cmd(_LCD_CLEAR);
findshed(0);
Lcd_Out(1,1,"Shedule: S");
Lcd_Out(1,11,shednon);
Lcd_Out(2,1,"Count:");
WordToStr(count, countn);
Lcd_Out(2,1,"Count:");
Lcd_Out(2,8,countn);
Lcd_Out(3,1,"Steps:");
Lcd_Out(4,1,"Temp:");
}
void main() {
TRISA = 0b00000000;
TRISB = 0b11111111;
PORTA = 0b00000000;
ADCON0=0;
ADCON1=0X0F;
CMCON=0X07;
CCP1CON=0;
CCP2CON=0;
SSPCON1=0;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,6,"Welcome !");
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
disval();
EEPROM_Write(0x00,255);
for(i=1;i<131;i++){
EEPROM_Write(i,i);
Delay_ms(50);
}
while(1){
if(PORTB.B7 = 1){ //proximity count
Delay_ms(300);
count++;
disval();
}
//
if(PORTB.B5 = 1){ //proximity count reset
Delay_ms(300);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Reset Count?");
WordToStr(count, countn);
Lcd_Out(2,1,countn);
Lcd_Out(3,1,"Press Enter to Reset");
Lcd_Out(4,1,"Press Up to Back");
while(1){
if(PORTB.B4 = 1){ //enter
Delay_ms(300);
count=0;
disval();
break;
}else if(PORTB.B2 = 1){ //up
Delay_ms(300);
disval();
break;
}
}
}
//
if(PORTB.B1 = 1){ //settings
shedc = 0;
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Select Shedule: S");
findshed(0);
Lcd_Out(1,18,shednon);
Lcd_Out(2,1,"Select Set. to Back");
Lcd_Out(3,1,"Select Enter to Edit");
Lcd_Out(4,1,"Select Up or Down");
Lcd_Cmd(_LCD_FIRST_ROW);
for(i=1;i<17;i++){
Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
}
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
while(1){
if(PORTB.B1 = 1){ //exit
Delay_ms(300);
disval();
Lcd_Cmd(_LCD_CURSOR_OFF);
break;
}else if(PORTB.B2 = 1){ //up button
Delay_ms(300);
if(shedno < 20){
shedc++;
}
findshed(shedc);
Lcd_Out(1,18,shednon);
Lcd_Cmd(_LCD_FIRST_ROW);
for(i=1;i<17;i++){
Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
}
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
}else if(PORTB.B3 = 1){ //down button
Delay_ms(300);
if(shedno > 1){
shedc--;
}
findshed(shedc);
Lcd_Out(1,18,shednon);
Lcd_Cmd(_LCD_FIRST_ROW);
for(i=1;i<17;i++){
Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
}
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
}else if(PORTB.B4 = 1){ //enter button
Delay_ms(300);
findshed(shedc);
shedrelay();
editshed();
Lcd_Cmd(_LCD_SECOND_ROW);
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
while(1){
if(PORTB.B4 = 1){
Delay_ms(300);
Lcd_Cmd(_LCD_SECOND_ROW);
for(i=1;i<11;i++){
Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
}
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
while(1){
if(PORTB.B4 = 1){
Delay_ms(300);
Lcd_Cmd(_LCD_THIRD_ROW);
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
while(1){
if(PORTB.B4 = 1){
Delay_ms(300);
Lcd_Cmd(_LCD_THIRD_ROW);
for(i=1;i<11;i++){
Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
}
Lcd_Cmd(_LCD_BLINK_CURSOR_ON);
while(1){
}
}else if(PORTB.B2 = 1){//up
}else if(PORTB.B3 = 1){//down
}
}
}else if(PORTB.B2 = 1){//up
}else if(PORTB.B3 = 1){//down
}
}
}else if(PORTB.B2 = 1){//up
}else if(PORTB.B3 = 1){//down
}
}
}
}
}
//
if((PORTB.B2 = 1)||(PORTB.B3 = 1)){ //up or down
shedc = 0;
Delay_ms(300);
Lcd_Cmd(_LCD_CLEAR);
findshed(0);
Lcd_Out(1,1,"Runing: S");
Lcd_Out(1,10,shednon);
Lcd_Out(2,1,"Next:");
Lcd_Out(3,1,"Press Up or Down Key");
Lcd_Out(4,1,"Press Reset to Back");
while(1){
if(PORTB.B3 = 1){ //down
Delay_ms(300);
if(shedno > 1){
shedc--;
}
findshed(shedc);
shedrelay();
shedselectdis();
}else if(PORTB.B2 = 1){ //up
Delay_ms(300);
if(shedno < 20){
shedc++;
}
findshed(shedc);
shedrelay();
shedselectdis();
}else if(PORTB.B4 = 1){ //enter
Delay_ms(300);
findshed(shedc);
EEPROM_Write(0x00,256-shedno);
disval();
break;
}else if(PORTB.B6 = 1){ //test
Delay_ms(300);
PORTC = 0b00000000;
Delay_ms(100);
relayssw(reg1, reg2, reg3, reg4, reg5,reg6);
}else if(PORTB.B5 = 1){ //reset
Delay_ms(300);
disval();
break;
}
}
}
//
}
}