I wrote the following simple program to output a series of short pulses from all the pins of PORTC depending on an input to PORTB. To give "0000 0000" to PORTB as input I grounded all pins. To give "0000 0001", ground connection to RB0 pin was removed and nothing was connected. Is this the way to input "1" and "0" to the PIC ?
I checked the output from a pin of PORTC using an oscilloscope. I got the proper output only once. I couldn't even see a "0" in the oscilloscope when I tested again a several times. Please tell me whether I have written the program correctly. I'm not sure about giving inputs to the PIC as well.
IS THERE ANY OTHER WAY TO OUTPUT A SERIES OF SHORT PULSES UPON A GIVEN INPUT? I tried using PWM, but CCP1 pin did not go to zero when duty cycle was made 0. Logic used for branching was same as the following program. CCPRL1 was made zero to make duty cycle zero. Could you please tell me why this method doesn't work ?
Thanks.
#include <p18f452.h> /* for TRISC and PORTC declarations */
#include <delays.h>
/* Set configuration bits for use with ICD2 / PICDEM2 PLUS Demo Board:
* - set HS oscillator
* - disable watchdog timer
* - disable low voltage programming
*/
#pragma romdata CONFIG
_CONFIG_DECL(_CONFIG1H_DEFAULT & _OSC_HS_1H,\
_CONFIG2L_DEFAULT,\
_CONFIG2H_DEFAULT & _WDT_OFF_2H,\
_CONFIG3H_DEFAULT,\
_CONFIG4L_DEFAULT & _LVP_OFF_4L,\
_CONFIG5L_DEFAULT,\
_CONFIG5H_DEFAULT,\
_CONFIG6L_DEFAULT,\
_CONFIG6H_DEFAULT,\
_CONFIG7L_DEFAULT,\
_CONFIG7H_DEFAULT);
#pragma romdata
/*#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF */
/* connect all PINS of PORTB to ground to make PORTB = 0 */
//*********************Global Variables **********************
unsigned char temp_portb;
int i;
//************************************************************
void main(void)
{
TRISB = 0xff; // set PORTB as input
TRISC = 0x00; // PORTC pins set as output
temp_portb = 0x00; // initialize "temp_portb" to enter into the loop
while(1) // polling loop
{
PORTC = 0x00;
if(PORTB == 0x00)
{
PORTC = 0x00;
}
else if(PORTB == 0x01 && temp_portb != 0x01) // 0000 0001
{
for(i = 0; i < 10; i++)
{
PORTC = 0x00;
Delay1KTCYx(10); // 2ms
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}
else if(PORTB == 0x02 && temp_portb != 0x02) // 0000 0010
{
for(i = 0; i < 20; i++)
{
PORTC = 0x00;
Delay1KTCYx(10);
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}
else if(PORTB == 0x04 && temp_portb != 0x04) // 0000 0100
{
for(i = 0; i < 30; i++)
{
PORTC = 0x00;
Delay1KTCYx(10);
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}
else if(PORTB == 0x08 && temp_portb != 0x08) // 0000 1000
{
for(i = 0; i < 40; i++)
{
PORTC = 0x00;
Delay1KTCYx(10);
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}
else if(PORTB == 0x10 && temp_portb != 0x10) // 0001 0000
{
for(i = 0; i < 50; i++)
{
PORTC = 0x00;
Delay1KTCYx(10);
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}
temp_portb = PORTB; // each "for" loop is executed only once for an i/p
}
}
I checked the output from a pin of PORTC using an oscilloscope. I got the proper output only once. I couldn't even see a "0" in the oscilloscope when I tested again a several times. Please tell me whether I have written the program correctly. I'm not sure about giving inputs to the PIC as well.
IS THERE ANY OTHER WAY TO OUTPUT A SERIES OF SHORT PULSES UPON A GIVEN INPUT? I tried using PWM, but CCP1 pin did not go to zero when duty cycle was made 0. Logic used for branching was same as the following program. CCPRL1 was made zero to make duty cycle zero. Could you please tell me why this method doesn't work ?
Thanks.
#include <p18f452.h> /* for TRISC and PORTC declarations */
#include <delays.h>
/* Set configuration bits for use with ICD2 / PICDEM2 PLUS Demo Board:
* - set HS oscillator
* - disable watchdog timer
* - disable low voltage programming
*/
#pragma romdata CONFIG
_CONFIG_DECL(_CONFIG1H_DEFAULT & _OSC_HS_1H,\
_CONFIG2L_DEFAULT,\
_CONFIG2H_DEFAULT & _WDT_OFF_2H,\
_CONFIG3H_DEFAULT,\
_CONFIG4L_DEFAULT & _LVP_OFF_4L,\
_CONFIG5L_DEFAULT,\
_CONFIG5H_DEFAULT,\
_CONFIG6L_DEFAULT,\
_CONFIG6H_DEFAULT,\
_CONFIG7L_DEFAULT,\
_CONFIG7H_DEFAULT);
#pragma romdata
/*#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF */
/* connect all PINS of PORTB to ground to make PORTB = 0 */
//*********************Global Variables **********************
unsigned char temp_portb;
int i;
//************************************************************
void main(void)
{
TRISB = 0xff; // set PORTB as input
TRISC = 0x00; // PORTC pins set as output
temp_portb = 0x00; // initialize "temp_portb" to enter into the loop
while(1) // polling loop
{
PORTC = 0x00;
if(PORTB == 0x00)
{
PORTC = 0x00;
}
else if(PORTB == 0x01 && temp_portb != 0x01) // 0000 0001
{
for(i = 0; i < 10; i++)
{
PORTC = 0x00;
Delay1KTCYx(10); // 2ms
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}
else if(PORTB == 0x02 && temp_portb != 0x02) // 0000 0010
{
for(i = 0; i < 20; i++)
{
PORTC = 0x00;
Delay1KTCYx(10);
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}
else if(PORTB == 0x04 && temp_portb != 0x04) // 0000 0100
{
for(i = 0; i < 30; i++)
{
PORTC = 0x00;
Delay1KTCYx(10);
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}
else if(PORTB == 0x08 && temp_portb != 0x08) // 0000 1000
{
for(i = 0; i < 40; i++)
{
PORTC = 0x00;
Delay1KTCYx(10);
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}
else if(PORTB == 0x10 && temp_portb != 0x10) // 0001 0000
{
for(i = 0; i < 50; i++)
{
PORTC = 0x00;
Delay1KTCYx(10);
PORTC = 0xff;
Delay1KTCYx(10);
}
PORTC = 0x00;
}
temp_portb = PORTB; // each "for" loop is executed only once for an i/p
}
}