Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC microcontroller simple program help

Status
Not open for further replies.

numair_noor

Member level 2
Joined
Mar 15, 2012
Messages
50
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,715
hi, i m new to pic microcontroller. i am trying to program a pic microcontroller may be PIC18F458. for a simple timer program.
for the following question:
A timer based on 24 hours timer which has a switch to select off,
a)8 hrs on and 16 hrs off
b)10 hrs on and 14 hrs off
c)12 on and 12 hours off
d) 16 hrs on and 8 hrs off
e)18 hrs on and 6 hrs off
and on all the time which has LED light in indicate mode it is on.

The input and output is ranging of 100 to 240 VAC and should be able to handle a load about 120W which work out to about 1.2 amp at 100VAC.

this is the code i have written so far.
PROBLEMS:
1)i am new to pic:)
2)when i run this program this gives me error: "undeclared identifier PORTCbit in expression"
3)am i using the do-while loop correctly(i m asking this because i want to know if user changes switch from c to b, then it must break where ever it is. and go back to other mode.)
4)please tell me how to access bit wise. i m using mikroc pro 4.60.0.0
5)please also tell me the best and easy to install compiler. i have installed mplab ide v5.6 before. but it required some library called c18. i could not install it. so i deleted it. and install this one. if it is ok. thn please tell me the procedure to solve. em trying to torrent newer version of software mikroc pro thanks in advance.
Code:
//declaration for switches for input
#define sw0 PORTBbits.RB0
#define sw1 PORTBbits.RB1
#define sw2 PORTBbits.RB2
#define sw3 PORTBbits.RB3
#define sw4 PORTBbits.RB4
#define sw5 PORTBbits.RB5
#define sw6 PORTBbits.RB6
//declaration for led's and outputs
#define led0 PORTCbit.RC0
#define led1 PORTCbit.RC1
#define led2 PORTCbits.RC2
#define led3 PORTCbits.RC3
#define led4 PORTCbits.RC4
#define led5 PORTCbits.RC5
#define led6 PORTCbits.RC6
#define led7 PORTCbits.RC7

int main()
{
unsigned int a,c,d;
//char f;
 TRISB0.RB0=1;
 TRISB1.RB1=1;
 TRISB2.RB2=1;
 TRISB3.RB3=1;
 TRISB4.RB4=1;
 TRISB5.RB5=1;
 TRISB6.RB6=1;
 TRISB7.RB7=1;
 
 TRISC0.RC0=0;
 TRISC1.RC1=0;
 TRISC2.RC2=0;
 TRISC3.RC3=0;
 TRISC4.RC4=0;
 TRISC5.RC5=0;
 TRISC6.RC6=0;
 TRISC7.RC7=0;
 //TRISB=1;  //monitor port b as input
 //TRISC=0;  //monitor port c as output
             while(1)
             {
 //condition 1
              do
                {
                 led0=1;                   //led is indicatin the timer is in on mode
                 for(a=0;a<=7;a++)         //timer is on for 8 hours
                  {
                  for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                     delay_ms(900);
                     }
                  }
                   led0=0;                   //led indicating that timer is now in off mode
                   for(a=0;a<=15;a++)        //timer is off for 16 hours
                   {
                    for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                     delay_ms(900);
                     }
                    }

                 }while(sw0==1 && sw1==1 && sw2==0 && sw3==0 && sw4==0 && sw5==0 && sw6==0);

//cond2()

                 do
                {
                 led1=1;                   //led is indicatin the timer is in on mode
                 for(a=0;a<=9;a++)         //timer is on for 10 hours
                  {
                  for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                     delay_ms(900);
                     }
                  }
                   led1=0;                   //led indicating that timer is now in off mode
                   for(a=0;a<=13;a++)        //timer is off for 14 hours
                   {
                    for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                     delay_ms(900);
                     }
                    }

                 }while(sw0==1 && sw1==0 && sw2==1 && sw3==0 && sw4==0 && sw5==0 && sw6==0);

//cond3()

do
                {
                 led2=1;                   //led is indicatin the timer is in on mode
                 for(a=0;a<=11;a++)         //timer is on for 12 hours
                  {
                  for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                     delay_ms(900);
                     }
                  }
                   led2=0;                   //led indicating that timer is now in off mode
                   for(a=0;a<=11;a++)        //timer is off for 12 hours
                   {
                    for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                     delay_ms(900);
                     }
                    }

                 }while(sw0==1 && sw1==0 && sw2==0 && sw3==1 && sw4==0 && sw5==0 && sw6==0);


//cond4()

do
                {
                 led3=1;                   //led is indicatin the timer is in on mode
                 for(a=0;a<=15;a++)         //timer is on for 16 hours
                  {
                  for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                     delay_ms(900);
                     }
                  }
                   led3=0;                   //led indicating that timer is now in off mode
                   for(a=0;a<=7;a++)        //timer is off for 8 hours
                   {
                    for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                     delay_ms(900);
                     }
                    }

                 }while(sw0==1 && sw1==0 && sw2==0 && sw3==0 && sw4==1 && sw5==0 && sw6==0);


//cond5()

do
                {
                 led4=1;                   //led is indicatin the timer is in on mode
                 for(a=0;a<=17;a++)         //timer is on for 18 hours
                  {
                  for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                     delay_ms(900);
                     }
                  }
                   led4=0;                   //led indicating that timer is now in off mode
                   for(a=0;a<=5;a++)        //timer is off for 6 hours
                   {
                    for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                     delay_ms(900);
                     }
                    }

                 }while(sw0==1 && sw1==0 && sw2==0 && sw3==0 && sw4==0 && sw5==1 && sw6==0);
}
 

One error is because you have used

#define led0 PORTCbit.RC0

instead of

#define led0 PORTCbits.RC0

Note the "s" in "bits".

Rather than using illegal copies of software I would suggest you consider Microchip's offering - MPLAB with C18. They are free - they only thing you lose on the free versions are optimisations which I doubt will concern you for now.

Keith
 

still same error...
i think the problem is in while(condition) statement.. as the compiler shows me...:(
 

Post a copy of the error message and location.

Also, check your curly braces - they seem mismatched - you need one more at the end.

Keith
 

0 1 mikroCPIC1618.exe -MSF -DBG -pP18F458 -DL -O11111114 -fo8 -N"C:\Users\numair\Desktop\mikroc project\numair.mcppi" -SP"C:\Users\Public\Documents\Mikroelektronika\mikroC PRO for PIC\defs\" -SP"C:\Users\Public\Documents\Mikroelektronika\mikroC PRO for PIC\uses\P18\" -SP"C:\Users\numair\Desktop\mikroc project\" "numair.c" "__Lib_Math.mcl" "__Lib_MathDouble.mcl" "__Lib_System.mcl" "__Lib_Delays.mcl" "__Lib_CType.mcl" "__Lib_CString.mcl" "__Lib_CStdlib.mcl" "__Lib_CMath.mcl" "__Lib_Conversions.mcl" "__Lib_Sprintf.mcl" "__Lib_PrintOut.mcl" "__Lib_Sprinti.mcl" "__Lib_Sprintl.mcl" "__Lib_Time.mcl" "__Lib_Trigonometry.mcl" "__Lib_Button.mcl" "__Lib_Keypad4x4.mcl" "__Lib_Manchester.mcl" "__Lib_OneWire.mcl" "__Lib_PS2.mcl" "__Lib_Sound.mcl" "__Lib_SoftI2C.mcl" "__Lib_SoftSPI.mcl" "__Lib_SoftUART.mcl" "__Lib_ADC_A_C.mcl" "__Lib_EEPROM_256.mcl" "__Lib_FLASH_w8_e64.mcl" "__Lib_I2C_c34.mcl" "__Lib_PWM_c2d4.mcl" "__Lib_SPI_c345.mcl" "__Lib_UART_c67.mcl" "__Lib_CAN_C_A.mcl" "__Lib_PortExpander.mcl" "__Lib_CANSPI.mcl" "__Lib_CF.mcl" "__Lib_CFFat16.mcl" "__Lib_GlcdFonts.mcl" "__Lib_Glcd.mcl" "__Lib_LcdConsts.mcl" "__Lib_Lcd.mcl" "__Lib_Mmc.mcl" "__Lib_MmcFat16.mcl" "__Lib_RS485.mcl" "__Lib_S1D13700.mcl" "__Lib_T6963C.mcl" "__Lib_SPIGlcd.mcl" "__Lib_SPILcd.mcl" "__Lib_SPILcd8.mcl" "__Lib_SPIT6963C.mcl" "__Lib_EthEnc28j60.mcl" "__Lib_TFT.mcl" "__Lib_TouchPanel_TFT.mcl" "__Lib_EthEnc24j600.mcl" "__Lib_TouchPanel.mcl"
0 126 All files Preprocessed in 31 ms
0 122 Compilation Started numair.c
47 324 Undeclared identifier 'PORTCbits' in expression numair.c
56 324 Undeclared identifier 'PORTCbits' in expression numair.c
66 324 Undeclared identifier 'PORTBbits' in expression numair.c
66 324 Undeclared identifier 'PORTBbits' in expression numair.c
66 324 Undeclared identifier 'PORTBbits' in expression numair.c
66 324 Undeclared identifier 'PORTBbits' in expression numair.c
66 324 Undeclared identifier 'PORTBbits' in expression numair.c
66 324 Undeclared identifier 'PORTBbits' in expression numair.c
66 324 Undeclared identifier 'PORTBbits' in expression numair.c
72 324 Undeclared identifier 'PORTCbits' in expression numair.c
81 324 Undeclared identifier 'PORTCbits' in expression numair.c
91 324 Undeclared identifier 'PORTBbits' in expression numair.c
91 324 Undeclared identifier 'PORTBbits' in expression numair.c
91 324 Undeclared identifier 'PORTBbits' in expression numair.c
91 324 Undeclared identifier 'PORTBbits' in expression numair.c
91 324 Undeclared identifier 'PORTBbits' in expression numair.c
91 324 Undeclared identifier 'PORTBbits' in expression numair.c
91 324 Undeclared identifier 'PORTBbits' in expression numair.c
97 324 Undeclared identifier 'PORTCbits' in expression numair.c
106 324 Undeclared identifier 'PORTCbits' in expression numair.c
116 324 Undeclared identifier 'PORTBbits' in expression numair.c
116 324 Undeclared identifier 'PORTBbits' in expression numair.c
116 324 Undeclared identifier 'PORTBbits' in expression numair.c
116 324 Undeclared identifier 'PORTBbits' in expression numair.c
116 324 Undeclared identifier 'PORTBbits' in expression numair.c
116 324 Undeclared identifier 'PORTBbits' in expression numair.c
116 324 Undeclared identifier 'PORTBbits' in expression numair.c
123 324 Undeclared identifier 'PORTCbits' in expression numair.c
132 324 Undeclared identifier 'PORTCbits' in expression numair.c
142 324 Undeclared identifier 'PORTBbits' in expression numair.c
142 324 Undeclared identifier 'PORTBbits' in expression numair.c
142 324 Undeclared identifier 'PORTBbits' in expression numair.c
142 324 Undeclared identifier 'PORTBbits' in expression numair.c
142 324 Undeclared identifier 'PORTBbits' in expression numair.c
142 324 Undeclared identifier 'PORTBbits' in expression numair.c
142 324 Undeclared identifier 'PORTBbits' in expression numair.c
149 324 Undeclared identifier 'PORTCbits' in expression numair.c
158 324 Undeclared identifier 'PORTCbits' in expression numair.c
168 324 Undeclared identifier 'PORTBbits' in expression numair.c
168 324 Undeclared identifier 'PORTBbits' in expression numair.c
168 324 Undeclared identifier 'PORTBbits' in expression numair.c
168 324 Undeclared identifier 'PORTBbits' in expression numair.c
168 324 Undeclared identifier 'PORTBbits' in expression numair.c
168 324 Undeclared identifier 'PORTBbits' in expression numair.c
168 324 Undeclared identifier 'PORTBbits' in expression numair.c
0 102 Finished (with errors): 07 Aug 2012, 13:38:18 numair.mcppi
 

I guess Mikroc doesn't have those port definitions. They are the ones that Microchip C18 uses. the easiest solution is probably simply manipulate the ports directly so for SW0 something like (PORTB & 0x01) etc.

Keith.
 

do you know any specific book regarding this pic18f458 i need to get my hands onto it.

- - - Updated - - -

do you know any specific book regarding this pic18f458 i need to get my hands onto it.
 

uptil now, i have written this code. but it only gives me the output at portb^0 (led1)= 1 all the time when input is portc^0(sw0)=1, by changing any of these inputs, this bit goes on blinking. but i need to make a timer based on these inputs. please help
Code:
#define led0 PORTB.F0
#define led1 PORTB.F1
#define led2 PORTB.F2
#define led3 PORTB.F3
#define led4 PORTB.F4
//#define led5 PORTB.F5

#define sw0 PORTC.F0
#define sw1 PORTC.F1
#define sw2 PORTC.F2
#define sw3 PORTC.F3
#define sw4 PORTC.F4
#define sw5 PORTC.F5
#define sw6 PORTC.F6
//#define sw7 PORTC.F7


int main()
{
unsigned int a,c,d;

 TRISB=0;    //monitor port c as output
//PORTB=0;
 TRISC=1;    //monitor port b as input
// PORTC=1;
             while(1)
             {
 //condition 1
 S:
             do
             {
                       PORTB=0;
             }        while(sw0==0 && sw1==0 && sw2==0 && sw3==0 && sw4==0 && sw5==0 && sw6==0);
              do
                {
                  PORTB.F0=1;              //led is indicatin the timer is in on mode
                  for(a=0;a<=7;a++)         //timer is on for 8 hours
                  {
                      for(c=0;c<=59;c++)
                     {
                        for(d=0;d<=59;d++)
                        {
                        if(sw0==1 && sw1==1 && sw2==0 && sw3==0 && sw4==0 && sw5==0 && sw6==0)
                         delay_ms(900);
                        else
                         goto S;
                        }
                     }
                     
                  }
                   PORTB.F0=0;                   //led indicating that timer is now in off mode
                   for(a=0;a<=15;a++)        //timer is off for 16 hours
                   {
                    for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                        {
                        if(sw0==1 && sw1==1 && sw2==0 && sw3==0 && sw4==0 && sw5==0 && sw6==0)
                         delay_ms(900);
                        else
                         goto S;
                        }
                     }
                    }

                 }while(sw0==1 && sw1==1 && sw2==0 && sw3==0 && sw4==0 && sw5==0 && sw6==0);

//cond2()

                 do
                {
                 PORTB.F1=1;                   //led is indicatin the timer is in on mode
                 for(a=0;a<=9;a++)         //timer is on for 10 hours
                  {
                  for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                        {
                        if(sw0==1 && sw1==0 && sw2==1 && sw3==0 && sw4==0 && sw5==0 && sw6==0)
                         delay_ms(900);
                        else
                         goto S;
                        }
                     }
                  }
                   PORTB.F1=0;                   //led indicating that timer is now in off mode
                   for(a=0;a<=13;a++)        //timer is off for 14 hours
                   {
                    for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                        {
                        if(sw0==1 && sw1==0 && sw2==1 && sw3==0 && sw4==0 && sw5==0 && sw6==0)
                         delay_ms(900);
                        else
                         goto S;
                        }
                     }
                    }

                 }while(sw0==1 && sw1==0 && sw2==1 && sw3==0 && sw4==0 && sw5==0 && sw6==0);

//cond3()

do
                {
                 PORTB.F2=1;                   //led is indicatin the timer is in on mode
                 for(a=0;a<=11;a++)         //timer is on for 12 hours
                  {
                  for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                        {
                        if(sw0==1 && sw1==0 && sw2==0 && sw3==1 && sw4==0 && sw5==0 && sw6==0)
                         delay_ms(900);
                        else
                         goto S;
                        }
                     }
                  }
                   PORTB.F2=0;                   //led indicating that timer is now in off mode
                   for(a=0;a<=11;a++)        //timer is off for 12 hours
                   {
                    for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                        {
                        if(sw0==1 && sw1==0 && sw2==0 && sw3==1 && sw4==0 && sw5==0 && sw6==0)
                         delay_ms(900);
                        else
                         goto S;
                        }
                     }
                    }

                 }while(sw0==1 && sw1==0 && sw2==0 && sw3==1 && sw4==0 && sw5==0 && sw6==0);


//cond4()

do
                {
                 PORTB.F3=1;                   //led is indicatin the timer is in on mode
                 for(a=0;a<=15;a++)         //timer is on for 16 hours
                  {
                  for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                        {
                        if(sw0==1 && sw1==0 && sw2==0 && sw3==0 && sw4==1 && sw5==0 && sw6==0)
                         delay_ms(900);
                        else
                         break;
                        }
                     }
                  }
                   PORTB.F3=0;                   //led indicating that timer is now in off mode
                   for(a=0;a<=7;a++)        //timer is off for 8 hours
                   {
                    for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                        {
                        if(sw0==1 && sw1==0 && sw2==0 && sw3==0 && sw4==1 && sw5==0 && sw6==0)
                         delay_ms(900);
                        else
                         goto S;
                        }
                     }
                    }

                 }while(sw0==1 && sw1==0 && sw2==0 && sw3==0 && sw4==1 && sw5==0 && sw6==0);


//cond5()

do
                {
                 PORTB.F4=1;                   //led is indicatin the timer is in on mode
                 for(a=0;a<=17;a++)         //timer is on for 18 hours
                  {
                  for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                        {
                        if(sw0==1 && sw1==0 && sw2==0 && sw3==0 && sw4==0 && sw5==1 && sw6==0)
                         delay_ms(900);
                        else
                         goto S;
                        }
                     }
                  }
                   PORTB.F4=0;                   //led indicating that timer is now in off mode
                   for(a=0;a<=5;a++)        //timer is off for 6 hours
                   {
                    for(c=0;c<=59;c++)
                     {
                     for(d=0;d<=59;d++)
                        {
                        if(sw0==1 && sw1==0 && sw2==0 && sw3==0 && sw4==0 && sw5==1 && sw6==0)
                         delay_ms(900);
                        else
                         goto S;
                        }
                     }
                    }

                 }while(sw0==1 && sw1==0 && sw2==0 && sw3==0 && sw4==0 && sw5==1 && sw6==0);
}
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top