Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Unable to change gpio (port) values

Status
Not open for further replies.

srikanthind

New Member
Can anyone run below of this code and could you please find out why is GPIO values can't be changed.
thank you

Code:
#include<pic.h>
#include<htc.h>
#define _XTAL_FREQ 4000000

__CONFIG(0x31C4);

//#use delay (clock = 4000000);

void _delay_ms(unsigned long n);

unsigned int cnt;

#define GREEN 0b00010000

#define trisio_cargreen 0b11101101
#define GPIO_cargreen 0b00001100
#define trisio_pedred 0b11101101
#define GPIO_pedred 0b000001010

void interrupt Isr(void)

{
  if (GPIF)

   { 
        if(GPIO3==0)
         {
           for (cnt=0; cnt<=5; cnt++)  // prevent debouncing
             { 
               __delay_ms(1);
               if((GPIO3)==0)
               cnt=0;
             } 
         
         GPIO |= (1<<2);    // LED ON
        
          for(cnt=0; cnt<100;cnt++) // 10 seconds delay
         __delay_ms(100);
 
         GPIO |= (1<<5);   // AMBER ON
           // 
         for(cnt=0; cnt<20;cnt++)   // 2 seconds delay
         __delay_ms(100);   
 

          GPIO &= ~(1<<2); // PED Led off 
          GPIO |= (1<<1); // PED green on
          GPIO &= ~(1<<5);  // AMber off
          GPIO &= ~(1<<4); // cars green off and RED on

    for(cnt=0; cnt<50;cnt++) // 5 seconds delay
         __delay_ms(100);

          
        GPIO &= ~(1<<1);  // ped green off and RED on...

        GPIO |= (1<<5); // cars amber on..

       for(cnt=0; cnt<30;cnt++) // 3 seconds delay
         __delay_ms(100);

      GPIO &= ~(1<<5); // cars amber off
      
      GPIO |= (1<<4);     // Cars green on and RED off
   }
  }
    GPIF=0;


} 
    

void main(void)
{

  ANSEL = 0b00000000;
	OPTION = 0b11010001;
	CMCON = 0b00000000;
    
TRISIO= 0B11111100;
     GIE = 1;
     GPIE=1;
    GPIF=0;
    IOCB3=1; 
GPIO3=1;

//GPIO=0b111111;

while(1)

{
    TRISIO= trisio_pedred;

   // GPIO=GPIO_pedred;
    GPIO |= (1<<1); 

   TRISIO= trisio_cargreen;
   //GPIO=GPIO_cargreen;
   //GPIO=0b00001100;

   GPIO |= (1<<2);

   //TRISIO= 0B00001000;
}
      
 
  

 

   // while(1)
       {}
             
}
 
Your TRIS values inside the main WHILE loop are identical. There's no need to keep assigning it. Assign it once outside of the loop, and then leave it.

But it looks like the main problem is that you're changing GPIO output states very fast. Maybe it just looks like they're not changing.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top