Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 24th June 2009, 08:03 PM   #1
Default help in programming using micro c

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
girgis adly is offline  
Old 25th June 2009, 07:15 AM   #2
Default

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)
3v0 is offline  
Old 25th June 2009, 07:30 AM   #3
Default

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 by be80be; 25th June 2009 at 07:37 AM.
be80be is offline  
Old 30th June 2009, 04:46 PM   #4
Default

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 is offline  
Old 30th June 2009, 05:04 PM   #5
Default

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
girgis adly is offline  
Old 30th June 2009, 05:51 PM   #6
Default

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.
be80be is offline  
Old 1st July 2009, 12:54 PM   #7
Default

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 by girgis adly; 1st July 2009 at 01:01 PM.
girgis adly is offline  
Old 1st July 2009, 02:40 PM   #8
Default

Quote:
Originally Posted by girgis adly View Post
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.
BeeBop is offline  
Old 1st July 2009, 02:42 PM   #9
Default

LOL you posted at the same time BeeBop

Here you can read it the data sheet
Quote:
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://ww1.microchip.com/downloads/e...Doc/39564c.pdf
LOL you posted at the same time BeeBop

Last edited by be80be; 1st July 2009 at 02:53 PM.
be80be is offline  
Old 1st July 2009, 03:14 PM   #10
Default

a thorough reading of the datasheet is in great order.
millwood is offline  
Old 2nd July 2009, 05:46 PM   #11
Default

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
girgis adly is offline  
Old 2nd July 2009, 06:17 PM   #12
Default

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.
BeeBop is offline  
Old 2nd July 2009, 06:30 PM   #13
Default

i mean that the message appear on the lcd and the programe run without i press on any switch the switches has no effect
girgis adly is offline  
Old 2nd July 2009, 08:29 PM   #14
Default

Quote:
Originally Posted by girgis adly View Post
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)
BeeBop is offline  
Old 2nd July 2009, 09:46 PM   #15
Default

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.
be80be is offline  
Reply

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



All times are GMT. The time now is 05:38 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker