![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
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 | |
| |
| | #2 |
|
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.
__________________ Please post questions to the forums. PM's are for personal communication. BCHS/3v0's Tutorials Junebug USB PIC programmer kit., USB Bit Whacker, The 15 Minute Printed Circuit Board! (+drill time) | |
| |
| | #3 |
|
I bet this is it not set for digital Code:
ADCON1 =7
Code: if(porta.f0==0)
{goto menu;}
if(porta.f1==0)
{goto exit; }}
if(porta.f1==0)
Last edited by be80be; 25th June 2009 at 07:37 AM. | |
| |
| | #4 |
|
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()
Code: ADCON1=7 please reply thanks | |
| |
| | #5 |
|
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 | |
| |
| | #6 |
|
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 by be80be; 30th June 2009 at 05:55 PM. | |
| |
| | #7 |
| Code: ADCON1=7; what is the mean of this line also when the port is analog and when it digital Last edited by girgis adly; 1st July 2009 at 01:01 PM. | |
| |
| | #8 |
| 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.
| |
| |
| | #9 | |
|
LOL you posted at the same time BeeBop Here you can read it the data sheet Quote:
LOL you posted at the same time BeeBop Last edited by be80be; 1st July 2009 at 02:53 PM. | ||
| |
| | #10 |
|
a thorough reading of the datasheet is in great order.
| |
| |
| | #11 |
|
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();}
}
MEGAUPLOAD - The leading online storage and file delivery service please see it and try to help me in writing te right code | |
| |
| | #12 |
|
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 by BeeBop; 2nd July 2009 at 06:19 PM. | |
| |
| | #13 |
|
i mean that the message appear on the lcd and the programe run without i press on any switch the switches has no effect
| |
| |
| | #14 | |
| Quote:
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) | ||
| |
| | #15 |
|
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
put here what you want to happen; // negates value on PORTD
oldstate = 0;
}
} while(1); // endless loop
Last edited by be80be; 2nd July 2009 at 09:49 PM. | |
| |
|
| Tags |
| micro, programming |
| Thread Tools | |
| Display Modes | |
| |
Similar | ||||
| Title | Starter | Forum | Replies | Latest |
| pic programming by micro C | girgis adly | Micro Controllers | 4 | 28th April 2009 02:37 PM |
| pic micro programming in C | fusian | Micro Controllers | 8 | 18th February 2008 06:11 PM |
| programming of AT89c51 micro-controller | nivvy | Micro Controllers | 7 | 4th December 2005 05:57 AM |
| micro C OS | mithilaa | Micro Controllers | 0 | 21st January 2005 06:58 AM |
| MICRO | herbymcduff | Datasheet/Parts Requests | 3 | 20th December 2002 05:16 PM |