Simple C code and PIC microcontroller: need it looked at

In summary: For example, if LED is 1, then case 0 will be skipped, case 1 will be executed (including its break statement), and case 2 will be executed (including its break statement).The code is missing a break statement in the first if/else if statement, which could cause unexpected behavior.There is a missing semicolon after the while(true) statement.In summary, the conversation is about using a PIC 16F877A microcontroller to control the lights and a solenoid based on readings from an LDR and moisture sensor. The code includes functions for turning on and off the lights and activating the sol
  • #1
axe34
38
0

Homework Statement


I'm using a PIC 16F877A microcontroller. An LDR and moisture sensor connect to the ADC. The main body of the code is ''void main'', and there is also a sub-code called "void light_the_led'' which is read inside the 'void main code'.

I cannot get access easily to hardware or software to test this code below. Does it look ok? Particularly is the ADC code set-up correctly?

Homework Equations


see code below

3. The Attempt at a Solution

see code below. NOTE: this microcontroller gives logic 0 when output is high, and 1 when low!

Thanks. I wish I had access to someone who could try this on a microcontroller (and use an led instead of solenoid).

Code:
/*****************************************************************************************************

Description: lights at pin A5 will come on when it is quite dark and/or quite wet. The lights at pin B4 will come on when it is very dark and/or very wet (and the lights at pin A5 will go off). No lights on when it is not dark enough or wet enough.A push type solenoid will active only when either of the lights (pin A5 or B4) come on.

**********************************************************************************************************/#include  <16f877a.h>  // compiler directives

#device  ICD=TRUE

#fuses  HS,NOLVP,NOWDT,PUT

#use delay (clock=20000000)
#define  SIDE_LIGHTS  PIN_A5  // define the pin allocations

#define  FULL_LIGHTS  PIN_B4

#define_SOLENOID PIN_E0#define  cutoff1 128    // quite dark (reading the LDR input)

#define  cutoff2 192    // very dark  (reading the LDR input)

#define cutoff3 128  //quite wet  (reading the moisture input)

#define cutoff4 192  //very wet  (reading the moisture input)
void light_the_led(int led)  // define a function to be read inside main function

{

Output_high(SIDE_LIGHTS);  // turn OFF led

Output_high(FULL_LIGHTS);  //turn led OFF
Switch(LED) 

{

  Case 0 : output_low(SIDE_LIGHTS);  //turn this light on

  Case 1 : output_low(FULL_LIGHTS);  //turn this light on

 

}}

void main( )

{

int reading; 

Setup_adc_ports  (RA1_RA2_Analog);  // set up ADC

Setup_adc  (ADC_CLOCK_INTERNAL);  // Setup ADC

 

while(true)

{

Set_adc_channel (1 ); 

reading = read_adc( 1);  //read LDR value

if (reading > cutoff1)

  light_the_led(0);

  output_low(SOLENOID);  //activates the push type solenoid

else if (reading > cuttoff2)

  light_the_led(1);

  output_low(SOLENOID);  //activates the push type solenoid

else

  output_high(SOLENOID);  //switches off the solenoidSet_adc_channel ( 2 ); 

reading = read_adc(2);  //read moisture sensor value

if (reading > cuttoff3)

  light_the_led(0);

{ Set_adc_channel (1)

if (reading > cuttoff2)

light_the_led(1); }

  output_low(SOLENOID);  //activates the push type solenoid

else if (reading > cuttoff4)

  light_the_led(1);

  output_low(SOLENOID);  //activates the push type solenoid

else

  output_high(SOLENOID);  //switches off the solenoid

  }

}
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
First, int main not void main. Be standard.

Maybe pasting this into the forum has mangled some things. Maybe it capitalized some symbols that should not be. I seem to remember C being case sensitive. It looks like you are passing in the argument led, but using the variable LED. Also, maybe you could make better use of white space and indents and such. But maybe that got mangled by pasting here.

Think about defining symbols for the turn-on and turn-off values. Possibly you want to make them Booleans if you are confident that you will never get any other state for an LED than on and off.

I always have to look up the behaviour of the switch/case statement. When you get case 0, does it do the code in case 0 then also do the code in case 1? Do you want a break statement in there? What happens if LED (or is it led) is other than 1 or 0?

There are a ton of things here that seem to be referring to the speciality library evoked by #include <16f877a.h>. I have no way to see if any of those is done correctly. All the items about output_high and the references to ADC. Those I have no way to check.

I see the while(true) idiom. This can be useful. But I see no exit from this loop. So this program looks like it will run forever. Is that intended? How do you get out? Usually when the while(true) is used, there is something in the loop that breaks you out of the loop on some pre-defined condition.
 
  • #3
code looks good. Its PIC's version of C.
 
  • #4
Is there a simulator for the PIC?
 
  • #5
(Fixed some of the bolding and added code tags to the OP)...
 
  • #6
DEvens said:
First, int main not void main. Be standard.

Maybe pasting this into the forum has mangled some things. Maybe it capitalized some symbols that should not be. I seem to remember C being case sensitive. It looks like you are passing in the argument led, but using the variable LED. Also, maybe you could make better use of white space and indents and such. But maybe that got mangled by pasting here.

Think about defining symbols for the turn-on and turn-off values. Possibly you want to make them Booleans if you are confident that you will never get any other state for an LED than on and off.

I always have to look up the behaviour of the switch/case statement. When you get case 0, does it do the code in case 0 then also do the code in case 1? Do you want a break statement in there? What happens if LED (or is it led) is other than 1 or 0?

There are a ton of things here that seem to be referring to the speciality library evoked by #include <16f877a.h>. I have no way to see if any of those is done correctly. All the items about output_high and the references to ADC. Those I have no way to check.

I see the while(true) idiom. This can be useful. But I see no exit from this loop. So this program looks like it will run forever. Is that intended? How do you get out? Usually when the while(true) is used, there is something in the loop that breaks you out of the loop on some pre-defined condition.
In addition to what DEvens said,
  • C is case-sensitive. Switch should be switch, and Case should be case.
  • With regard to the switch statement, if the switch variable matches a case label, the block of code in the case label executes, and every statement after that block executes, unless there is a break statement.
  • Also in the switch statement, LED is not declared. is no variable named LED. There is a parameter named led in your light_the_led function.
  • It's difficult to know what your intent is in this code in main():
    C:
    if (reading > cutoff1)
      light_the_led(0);
      output_low(SOLENOID);  //activates the push type solenoid
    else if (reading > cuttoff2)
      light_the_led(1);
      output_low(SOLENOID);  //activates the push type solenoid
    else
      // etc.
    berkeman added some indentation that you didn't have, but as the code exists now, I don't think it will compile. The indentation suggests that output_low() gets called if reading > cutoff1 or if reading > cutoff2. That's not the case. If you intend the two output_low() calls to be executed as part of the if and else if clauses, you must put braces around each block.
 

1. How can I improve the efficiency of my C code for a PIC microcontroller?

To improve the efficiency of your C code for a PIC microcontroller, you can use techniques such as optimizing loops, minimizing the use of floating-point operations, and using efficient data structures. Additionally, you can use compiler-specific optimizations and make use of hardware features of the PIC microcontroller.

2. How can I debug my C code for a PIC microcontroller?

There are various ways to debug C code for a PIC microcontroller, such as using a debugger tool provided by the manufacturer, using a simulator to test code before running it on the actual microcontroller, and using printf() statements to print out values during runtime. It is also helpful to have a good understanding of the microcontroller's architecture and instruction set to identify potential errors.

3. What are some common mistakes to avoid when writing C code for a PIC microcontroller?

Some common mistakes to avoid when writing C code for a PIC microcontroller include not properly understanding the microcontroller's architecture and instruction set, not considering the limited resources (such as memory and processing power) of the microcontroller, and not properly handling interrupts and timing issues.

4. How can I interface my C code with external devices using a PIC microcontroller?

To interface C code with external devices using a PIC microcontroller, you can use techniques such as using the microcontroller's GPIO (General Purpose Input/Output) pins, utilizing communication protocols such as SPI or I2C, and using specialized modules or shields designed for specific devices. It is important to properly configure the microcontroller's pins and ensure compatibility with the external device's specifications.

5. Are there any online resources or communities for troubleshooting issues with C code and PIC microcontrollers?

Yes, there are various online resources and communities dedicated to helping individuals troubleshoot issues with C code and PIC microcontrollers. Some popular resources include manufacturer forums, online communities and forums specifically for embedded systems and microcontrollers, and websites with tutorials and tips for working with PIC microcontrollers. It can also be helpful to consult with other experienced C programmers or reach out to the manufacturer for support.

Similar threads

  • Electrical Engineering
Replies
6
Views
1K
  • Electrical Engineering
Replies
6
Views
3K
Back
Top