C problem regarding PIC programming

Discussion in 'Programmer's Corner' started by San_Dendlek, Apr 6, 2010.

  1. San_Dendlek

    Thread Starter New Member

    Mar 24, 2010
    11
    0
    Hi I have a problem I am doing a program which changes the patterns. The following is the program.

    Code ( (Unknown Language)):
    1. #include <pic.h>
    2. #include <htc.h>
    3. #include "delay.c" 
    4. #include "delay.h" 
    5.  
    6. #define XTAL_FREQ 4MHZ
    7.  
    8. __CONFIG (WDTDIS & XT & UNPROTECT & PWRTEN);
    9.  
    10. void init(void)
    11. {
    12. // port directions: 1=input, 0=output
    13.  PORTA=0xFF;   
    14.  //PORTA=0x00; 
    15.  PORTB=0x00;
    16.  //PORTB=0xFF
    17.  TRISA=0xFF;
    18.  //TRISA=0x00;
    19.  TRISB=0x00;
    20.  //TRISB=0xFF;
    21. }
    22.  
    23.  
    24. void main(void)
    25. {
    26.     init();
    27.     while (1)
    28.     {
    29.         int check = PORTA;
    30.  
    31.         if(check == 0b00001)
    32.         {
    33.             goto start;
    34.         }
    35.        
    36.         if(check == 0b00010)
    37.         {
    38.             goto start1;
    39.         }
    40.        
    41.         if(check == 0b00100)
    42.         {
    43.             goto start2;
    44.         }
    45.  
    46.         if(check == 0b01000)
    47.         {
    48.             goto start3;
    49.         }
    50.  
    51.         start:
    52.         do
    53.         {
    54.             PORTB=0b11000000;
    55.             DelayMs(100);
    56.             for(int x=0; x<5; x++)
    57.             {
    58.                 PORTB=PORTB>>1;
    59.                 DelayMs(300);
    60.             }
    61.        
    62.             PORTB=0b00000011;
    63.             DelayMs(100);
    64.             for (int y=0; y<5; y++)
    65.             {
    66.                 PORTB=PORTB<<1;
    67.                 DelayMs(300);
    68.             }
    69.         }
    70.         while(check!=0b00010 && check!=0b00100 && check!=0b01000);
    71.    
    72.         start1:
    73.         do
    74.         {
    75.             PORTB=0b00000001;
    76.             DelayMs(200);
    77.             PORTB=0b00000011;
    78.             DelayMs(200);
    79.             PORTB=0b00000111;
    80.             DelayMs(200);
    81.             PORTB=0b00001111;
    82.             DelayMs(200);
    83.             PORTB=0b00011111;
    84.             DelayMs(200);
    85.             PORTB=0b00111111;
    86.             DelayMs(200);
    87.             PORTB=0b01111111;
    88.             DelayMs(200);
    89.             PORTB=0b11111111;
    90.             DelayMs(200);
    91.         }
    92.         while(check!=0b00001 && check!=0b00100 && check!=0b01000);
    93.    
    94.         start2:
    95.         do
    96.         {
    97.             PORTB=rand();
    98.             DelayMs(300);
    99.         }
    100.         while(check!=0b00001 && check!=0b00010 && check!=0b01000);
    101.    
    102.    
    103.         start3:
    104.         do
    105.         {
    106.             for(int x=0; x<256; x++)
    107.             {
    108.                 PORTB=x;
    109.                 DelayMs(30);
    110.             }
    111.         }
    112.         while(check!=0b00001 && check!=0b00010 && check!=0b00100);
    113.     }
    114. }
    115.    
    Now can anyone help me I want the program that when I press a switch I want it to continue it`s respective pattern until another switch is pressed
    and then does the pattern according to that switch. an so on and so forth

    any help is appreciated thanks a lot the PIC I am programming in 16F84A
     
  2. jase951

    New Member

    Jan 19, 2009
    1
    0
    Without looking at it all.

    I gather all your switches are connected to PORTA and all lights/whatever are connected to PORTB.

    First of all, would it not have been easier to use a switch() for checking which button is pressed and you can do away with all the conditional statements by putting the 'actions' within the switch().

    Are these momentary switches? If so, store the button # that has been pressed in a variable and use this as a check for which button has been last pressed in the switch(), therefore unless this variable has been changed the action upon each forever while loop will be the same.

    Jase
     
  3. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Take a good look at this loop:
    Code ( (Unknown Language)):
    1.  
    2.   do
    3.   {
    4.       // ...a bunch of stuff that doesn't involve changing "check"
    5.   }
    6.   while(check!=0b00001 && check!=0b00100 && check!=0b01000);
    7.  
    You seem to be waiting for something to change the variable "check" but nothing inside this loop will ever do so.

    Switches are conventionally connected to Gnd, but yours must pull high when the switch is pressed, with pulldown resistors. Have you wired them that way?

    And do you intend that if the program could ever leave the loop at start1, it would fall into the loop at start2, and when it left that, it would fall into start3?
     
  4. Harrington

    New Member

    Dec 19, 2009
    86
    3
    Johns got a good point here the loop that you are writing never ever picks up on switch change It might be better if you handled your switches through hardware interrupts or a timer interrupt perhaps

    I also notice that you are not using the

    Code ( (Unknown Language)):
    1.  
    2. switch(check)
    3.  
    4. {
    5.  case 4:
    6.  
    7. // code for this case
    8.  
    9.  case 2:
    10.  
    11. // code for this case
    12.  
    13. default :
    14.  
    15. // code for this case
    16.  
    17.  
    18.  
    19. }
    20.  
    21.  
    22.  
    Further down in this statement you can improve the way that you do this quite considerably

    Code ( (Unknown Language)):
    1.  
    2. start1: // Your code is left shifting one place and then "Bit-wise  OR " 1 to the port
    3.           // your doing this 8 times
    4.  
    5.         // your example as follows
    6.         do
    7.         {
    8.             PORTB=0b00000001;
    9.             DelayMs(200);
    10.             PORTB=0b00000011;
    11.             DelayMs(200);
    12.             // and so on and so on for each line
    13.  
    14.            
    15.         }
    16.  
    17.       /************ Far better way of achieving this below ********/
    18.  
    19.     unsigned char mask = 0x01 ;
    20.     portb = 0x01 ;
    21.  
    22.     for (int i = 0 ; i < 7 ; i ++ )
    23.    
    24.     {
    25.    
    26.     portb = portb << 1 ; // left shift by one place
    27.  
    28.    // bitwise OR Portb with mask after shifting you wont see this happen
    29.     portb = portb | mask ;
    30.     delay_us(200);
    31.     } // end for loop
    32.  
    33.  
    34.  
    35.  
    Much better than all those lines of code
     
Loading...