• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

help in programming using micro c

Status
Not open for further replies.

girgis adly

New Member
dear all
i write a cod for my project which is multilevel inverter equipped with lcd screen but when i check the code on a simulation programe (proteus)it is not work as intended this is the code written in micro c language and i use pic18f452 switches s1&s2 connected to port A gates of mosfets of the inverter connected to port B lcd connected to port c
i want the prog to do that when i run the prog some message appear on the lcd then message appear
menu
s1:5l s2:EX
this message appear and still on the screeen till i press s1 or s2 then if i press s1 prog run and message appear prog of 5l run and still on the screen untill the prog end as we use for loop after the prog end message appear
prog end
s1:menu s2:exit
if press s2 the prog go to exit and message appear
Exit
s1:yes s2:No
if press s1 mesage appear
prog end
good bye

now the problems in my code is the programe run without i press any switch and still run regardless of for loop


this is my code:


void main()
{
int (x) ;
lcd_init(&portc);
lcd_cmd(LCD_CURSOR_OFF);
lcd_out(1,1,"project of :");
delay_ms(2000);
lcd_cmd(lcd_clear);
lcd_out(1,4,"Multilevel ");
lcd_out(2,1," Pwm inverter ");
delay_ms(2000);
lcd_cmd(lcd_clear);
Lcd_Out_Cp("supervised by:");
lcd_out(2,1," Dr. Gamal Hashm");
delay_ms(3000);
lcd_cmd(lcd_clear);
lcd_Out_Cp("press s1 for run");
lcd_out(2,1,"press s2 for EX");
delay_ms(2000);
menu:
lcd_cmd(lcd_clear);
lcd_cmd(LCD_CURSOR_OFF);
Lcd_Out_Cp("menu: ");
lcd_out(2,1,"s1:5l s2:EXIT");

TRISB=0;
trisA=00001111;
if(portA.f0==0)
{
lcd_cmd(lcd_clear);
lcd_Out_Cp("prog of 5l run");
//code of project
for(x=0;x,1000;x++)
{
PORTB=0;
Delay_us(337);
PORTB=0B00000101;
Delay_us(2717);
PORTB=0B00001001;
delay_us(3889);
PORTB=0B00000101;
delay_us(2667);
PORTB=0;
delay_us(777);
PORTB=0B00000110;
delay_us(2667);
PORTB=0B00001010;
delay_us(3889);
PORTB=0B00000110;
delay_us(2667);
PORTB=0;
delay_us(388);
}
lcd_cmd(lcd_clear);
Lcd_Out_Cp(" prog end");
lcd_out(2,1,"s1:menu s2:exit");
if(porta.f0==0)
{goto menu;}
if(porta.f1==0)
{goto exit; }}
if(porta.f1==0)
{goto exit;}
exit:
Lcd_Out_Cp(" Exit");
lcd_out(2,1,"s1:yes s2:No");
if(porta.f0==0)
{
lcd_cmd(lcd_clear);
Lcd_Out_Cp(" prog end");
lcd_out(2,4," good bye");
}
if(porta.f1==0)
{goto menu;}}


I think to use keypad I write the code with keypad instead of switches but also the switch not run as intended in the simulation prog may be I connect the keypad in wrong way I try again by connecting switches instead of keypad but there is no use


This the code using keypad


void main()
{
int x;
char m;
keypad_init(&porta);
lcd_init(&portc);
lcd_cmd(LCD_CURSOR_OFF);
lcd_out(1,1,"project of :");
delay_ms(2000);
lcd_cmd(lcd_clear);
lcd_out(1,4,"Multilevel ");
lcd_out(2,1," Pwm inverter ");
delay_ms(2000);
lcd_cmd(lcd_clear);
Lcd_Out_Cp("supervised by:");
lcd_out(2,1," Dr. Gamal Hashm");
delay_ms(3000);
lcd_cmd(lcd_clear);
lcd_Out_Cp("press s1 for run");
lcd_out(2,1,"press s2 for EX");
delay_ms(2000);
menu:
lcd_cmd(lcd_clear);
lcd_cmd(LCD_CURSOR_OFF);
Lcd_Out_Cp("menu: ");
lcd_out(2,1,"s1:5l s2:EXIT");
TRISB=0;
m=keypad_read();
if(m==1)
{
lcd_cmd(lcd_clear);
lcd_Out_Cp("prog of 5l run");
//code of project
for(x=0;x,1000;x++)
{
PORTB=0;
Delay_us(337);
PORTB=0B00000101;
Delay_us(2717);
PORTB=0B00001001;
delay_us(3889);
PORTB=0B00000101;
delay_us(2667);
PORTB=0;
delay_us(777);
PORTB=0B00000110;
delay_us(2667);
PORTB=0B00001010;
delay_us(3889);
PORTB=0B00000110;
delay_us(2667);
PORTB=0;
delay_us(388);
}
lcd_cmd(lcd_clear);
Lcd_Out_Cp(" prog end");
lcd_out(2,1,"s1:menu s2:exit");
m=keypad_read();
if(m==1)
{goto menu;}
if(m==2)
{goto exit; }}
if(m==2)
{goto exit;}
exit:
Lcd_Out_Cp(" Exit");
lcd_out(2,1,"s1:yes s2:No");
m=keypad_read();
if(m==1)
{
lcd_cmd(lcd_clear);
Lcd_Out_Cp(" prog end");
lcd_out(2,4," good bye");
}
if(m==2)
{goto menu;}}

Please help me in correcting the code
Any suggestion will be appreciated
Thanks and regards
 

3v0

Coop Build Coordinator
Forum Supporter
You did not get a reply to you post because it is hard to understand.

The text is mostly very long run on sentences which are had to follow.

The code was not posted using the # icon which puts your code between [CODE]...your code here...[/CODE]. When you do so the code's formatting will be preserved making it much easier to read.

A schematic and or image of simulator showing how the IO is connected would help. Text descriptions of circuits are not good.

If the program runs without input then you must have a 0 on the port bits you test as switches. Try setting some breakpoints and examine the value.
 

be80be

Well-Known Member
I bet this is it not set for digital
Code:
         ADCON1 =7
Is this not the button checking part
Code:
       if(porta.f0==0)
           {goto menu;}
       if(porta.f1==0)
           {goto exit; }}
       if(porta.f1==0)
It's checking for low PORTA not set as digital will read low
 
Last edited:

girgis adly

New Member
dear all
this is the last code i wrote for the project now only the message appear on the lcd screen but the switches s1&s2has no effect the program not execut
when i press on any switch

Code:
void mymenu(void);
void main()
{
int x;

TRISB=0;
portb=0;
trisA=00001111;
lcd_init(&portc);
lcd_cmd(LCD_CURSOR_OFF);
lcd_out(1,1,"project of :");
delay_ms(2000);
lcd_cmd(lcd_clear);
lcd_out(1,4,"Multilevel ");
lcd_out(2,1," Pwm inverter ");
delay_ms(2000);
lcd_cmd(lcd_clear);
Lcd_Out_Cp("supervised by:");
lcd_out(2,1," Dr. Gamal Hashm");
delay_ms(3000);
lcd_cmd(lcd_clear);
lcd_Out_Cp("press s1 for run");
lcd_out(2,1,"press s2 for EX");
delay_ms(2000);


while(1);
         {                     // start of infinite loop
if(portA.f0==0)
{
lcd_cmd(lcd_clear);
lcd_Out_Cp("prog of 5l run");
//code of project
for(x=0;x < 1000;x++)
{
PORTB=0;
Delay_us(337);
PORTB=0B00000101;
Delay_us(2717);
PORTB=0B00001001;
delay_us(3889);
PORTB=0B00000101;
delay_us(2667);
PORTB=0;
delay_us(777);
PORTB=0B00000110;
delay_us(2667);
PORTB=0B00001010;
delay_us(3889);
PORTB=0B00000110;
delay_us(2667);
PORTB=0;
delay_us(388);
}
lcd_cmd(lcd_clear);
Lcd_Out_Cp(" prog end");
lcd_out(2,1,"s1:menu s2:exit");


if(porta.f0==0)
{goto menu;}
if(porta.f1==0)
{goto exit; }}
if(porta.f1==0)
{goto exit;}
exit:
Lcd_Out_Cp(" Exit");
lcd_out(2,1,"s1:yes s2:No");
if(porta.f0==0)
{
lcd_cmd(lcd_clear);
Lcd_Out_Cp(" prog end");
lcd_out(2,4," good bye");
}
if(porta.f1==0)
{goto menu;}
                  } // end while(1)
}

void mymenu() {
lcd_cmd(lcd_clear);
lcd_cmd(LCD_CURSOR_OFF);
Lcd_Out_Cp("menu: ");
lcd_out(2,1,"s1:5l s2:EXIT");
} // end mymenu()
dear sir be80be i didn't know how can i use the instruction
Code:
ADCON1=7
what it mean and where i use in my code
please reply
thanks
 

girgis adly

New Member
dear all i didn't know the instructions of analog to digital converter and what is the importance of using it here as i only want a set of instructions to execute when i press in a push buttom switch connected to the pic

alsoi want to ask if i need to make interrupt here (use interrupt function )if use please till me the instruction i need and where i write it in my code thanks
 

be80be

Well-Known Member
PortA is analogue so if you check your switches they will read wrong all the time.
Code:
void mymenu(void);
void main()
{
int x;
ADCON1=7;
TRISB=0;
portb=0;
trisA=00001111;
lcd_init(&portc);
lcd_cmd(LCD_CURSOR_OFF);
lcd_out(1,1,"project of :");
delay_ms(2000);
 
Last edited:

girgis adly

New Member
Code:
ADCON1=7;
why it equal 7 not any other number like 6 or 5
what is the mean of this line
also when the port is analog and when it digital
 
Last edited:

BeeBop

Active Member
Code:
ADCON1=7;
why it equal 7 not any other number like 6 or 5
what is the mean of this line
also when the port is analog and when it digital
If you look at your data sheet on page 182 there is a table for the ADCON1 register bits 0-3 <PCFG>. Look at the middle of the table. If bits 1 and 2 are set - 011x - the AD controller is off and that is what you want. 7 is b00000111 so that sets the correct bits to turn off the AD.
 

be80be

Well-Known Member
LOL you posted at the same time BeeBop

Here you can read it the data sheet
Depending on the device selected, there are either five
ports or three ports available. Some pins of the I/O
ports are multiplexed with an alternate function from
the peripheral features on the device. In general, when
a peripheral is enabled, that pin may not be used as a
general purpose I/O pin.
Each port has three registers for its operation. These
registers are:
• TRIS register (data direction register)
• PORT register (reads the levels on the pins of the
device)
• LAT register (output latch)
The data latch (LAT register) is useful for read-modifywrite
operations on the value that the I/O pins are
driving.
9.1 PORTA, TRISA and LATA
Registers
PORTA is a 7-bit wide, bi-directional port. The corresponding
Data Direction register is TRISA. Setting a
TRISA bit (= 1) will make the corresponding PORTA pin
an input (i.e., put the corresponding output driver in a
Hi-Impedance mode). Clearing a TRISA bit (= 0) will
make the corresponding PORTA pin an output (i.e., put
the contents of the output latch on the selected pin).
Reading the PORTA register reads the status of the
pins, whereas writing to it will write to the port latch.
The Data Latch register (LATA) is also memory
mapped. Read-modify-write operations on the LATA
register reads and writes the latched output value for
PORTA.
The RA4 pin is multiplexed with the Timer0 module
clock input to become the RA4/T0CKI pin. The RA4/
T0CKI pin is a Schmitt Trigger input and an open drain
output. All other RA port pins have TTL input levels and
full CMOS output drivers.
The other PORTA pins are multiplexed with analog
inputs and the analog VREF+ and VREF- inputs. The
operation of each pin is selected by clearing/setting the
control bits in the ADCON1 register (A/D Control
Register1).
The TRISA register controls the direction of the RA
pins, even when they are being used as analog inputs.
The user must ensure the bits in the TRISA register are
maintained set when using them as analog inputs.
Look at page 91 http://www.electro-tech-online.com/custompdfs/2009/07/39564c.pdf
LOL you posted at the same time BeeBop
 
Last edited:

girgis adly

New Member
dear all
thanks for all this useful information i will look at the data sheet
this is the last code i write i use analog to digital converter but there in no use
Code:
void mymenu(void);
void exit(void);
void main()
{
int x;
lcd_init(&portc);
lcd_cmd(LCD_CURSOR_OFF);
lcd_out(1,1,"project of :");
delay_ms(2000);
lcd_cmd(lcd_clear);
lcd_out(1,4,"Multilevel ");
lcd_out(2,1," Pwm inverter ");
delay_ms(2000);
lcd_cmd(lcd_clear);
Lcd_Out_Cp("supervised by:");
lcd_out(2,1," Dr. Gamal Hashm");
delay_ms(3000);
lcd_cmd(lcd_clear);
lcd_Out_Cp("press s1 for run");
lcd_out(2,1,"press s2 for EX");
delay_ms(2000);
PORTB = 0;              // Set PORTB to 0
TRISB = 0;              // PORTB is output

ADCON1 = 7;             // All ADC pins to digital I/O
PORTA = 255;
TRISA = 255;            // PORTA is input
if (Button(&PORTA, 0,1,1))   // button on RA0 pressed

{
lcd_cmd(lcd_clear);
lcd_Out_Cp("prog of 5l run");
//code of project
for(x=0;x < 1000;x++)
{
PORTB=0;
Delay_us(337);
PORTB=0B00000101;
Delay_us(2717);
PORTB=0B00001001;
delay_us(3889);
PORTB=0B00000101;
delay_us(2667);
PORTB=0;
delay_us(777);
PORTB=0B00000110;
delay_us(2667);
PORTB=0B00001010;
delay_us(3889);
PORTB=0B00000110;
delay_us(2667);
PORTB=0;
delay_us(388);
}
lcd_cmd(lcd_clear);
Lcd_Out_Cp(" prog end");
lcd_out(2,1,"s1:menu s2:exit");

if (Button(&PORTA, 0,1,1))   // button on RA0 pressed

{mymenu();}
if (Button(&PORTA, 1,1,1))   // button on RA1 pressed
{exit();}
}
if (Button(&PORTA, 1,1,1))   // button on RA1 pressed
{exit();}}

void mymenu() {
lcd_cmd(lcd_clear);
lcd_cmd(LCD_CURSOR_OFF);
Lcd_Out_Cp("menu: ");
lcd_out(2,1,"s1:5l s2:EXIT");
} // end mymenu()
void exit() {
Lcd_Out_Cp(" Exit");
lcd_out(2,1,"s1:yes s2:No");
if (Button(&PORTA, 0,1,1))   // button on RA0 pressed
{
lcd_cmd(lcd_clear);
Lcd_Out_Cp(" prog end");
lcd_out(2,4," good bye");
}
if (Button(&PORTA, 1,1,1))   // button on RA1 pressed
{mymenu();}
                  }
i upload the files of the project code and simulation file of proteus on this link
MEGAUPLOAD - The leading online storage and file delivery service
please see it and try to help me in writing te right code
 

BeeBop

Active Member
When you say 'there is no use' what exactly do you mean? I know you mean the program is not working, but HOW is it not working?

Are you able to put text on the LCD no problem? Is you problem just with the ADC? Button functions work well?

I don't have mikroC so cant download and test, perhaps Burt, or someone else?

EDIT: You are NOT using the ADC, yet... I take it your program just does nothing?
 
Last edited:

girgis adly

New Member
i mean that the message appear on the lcd and the programe run without i press on any switch the switches has no effect
 

BeeBop

Active Member
i mean that the message appear on the lcd and the programe run without i press on any switch the switches has no effect
OK, so it is your switch reading routines which are causing the problem. Can you just try to make a simple program to read 1 switch, and turn on an LED, without anything else?

I will try and read the mikro manual to figure out how this function button works. I'll be back later.
Button(&PORTA, 0,1,1)
 

be80be

Well-Known Member
Try this for you button routine see how it works
Code:
void main(){
  char oldstate = 0;
  TRISB = 0xFF;          // set PORTB to be input
  TRISD = 0;             // set PORTD to be output
  PORTD = 0x0F;          // initialize PORTD
  
  do {
    if (Button(&PORTB, 1, 1, 1))                // detect logical one on RB1 pin
      oldstate = 1;
    if (oldstate && Button(&PORTB, 1, 1, 0)) {  // detect one-to-zero transition on RB1 pin
      [COLOR="Red"]put here what you want to happen; [/COLOR] // negates value on PORTD
      oldstate = 0;
    }
  } while(1);         // endless loop
 
Last edited:

girgis adly

New Member
thanks very much

thanks for all for help me
this is the last code i write
Code:
void main(){
  char oldstate = 0;
  int x;
  TRISB = 0xFF;          // set PORTB to be input
  TRISD = 0;             // set PORTD to be output
  PORTD = 0x0F;          // initialize PORTD

  do {
    if (Button(&PORTB, 1, 1, 1))                // detect logical one on RB1 pin
      oldstate = 1;
    if (oldstate && Button(&PORTB, 1, 1, 0)) { 
                                                // detect one-to-zerotransition on RB1 pin
       for(x=0;x,1000;x++)
{
//code of my project
PORTd=0;
Delay_us(337);
PORTd=0B00000101;
Delay_us(2717);
PORTd=0B00001001;
delay_us(3889);
PORTd=0B00000101;
delay_us(2667);
PORTd=0;
delay_us(777);
PORTd=0B00000110;
delay_us(2667);
PORTd=0B00001010;
delay_us(3889);
PORTd=0B00000110;
delay_us(2667);
PORTd=0;
delay_us(388);
}


       // negates value on PORTD
      oldstate = 0;
    }
  } while(1);         // endless loop
  }
and when i press on the nswitch connected to pin RB1 the program run
iam very happy and thanks for all one try to help me
but was the error is that i was connect the switch to port A instead of port B
or what is the wrong in all the previous codes
thanks alot for all
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top