owh i have figured out where my fault is..
i forgot to define the sw1 as input at TRISB..
haha..
1 more thing..
how can i program so that 16F887 can do two task at the same time?
such as i want LED1 and LED2 at sk40c blinking,
while LED1-LED6 on another circuit do chase blink.
anyone can help?
i think it is about interrupt, isn't it?
this is my code.
i fail to do it.
- CODE: SELECT_ALL_CODE
//==========================================================================
// Author : CYTRON TECHNOLOGIES SDN BHD
// Project : SK40C sample code for PIC16F887
// Project description : Blink LED1 and LED2 like police car
//
//==========================================================================
// include
//==========================================================================
#include <htc.h>
#include "system.h"
// Configuration
//==========================================================================
__CONFIG(
FOSC_HS & // High Speed Crystal.
WDTE_OFF & // Disable Watchdog Timer.
PWRTE_ON & // Enable Power Up Timer.
BOREN_OFF & // Disable Brown Out Reset.
MCLRE_ON & // Enable MCLR
LVP_OFF
); // Disable Low Voltage Programming.
// function prototype
//==========================================================================
void delay_ms(unsigned int ui_value);
void LE(unsigned int light);
// main function
//==========================================================================
void main(void)
{
PORTA = 0; // Clear PORT
PORTB = 0;
PORTC = 0;
PORTD = 0;
PORTE = 0;
TRISA = 0b00000000; // set PORTA AS output
TRISB = 0b00000011; // set PORTB AS output
TRISC = 0b00001000; // set PORTC AS output
TRISD = 0b00000000; // set PORTD AS output
TRISE = 0b0001;
ANSEL = 0; // set PORTA as Digital I/O for PIC16F887
ANSELH = 0; // set PORTB as Digital I/O for PIC16F887
//LED1,LED2 blinking
LED1=1;
delay_ms(50);
LED1=0;
LED2=1;
delay_ms(50);
LED2=0;
if(SEN1)//sensor1 light up
{
LE(1);//led1-led6 at circuit chase blink
}
}
/*******************************************************************************
* PRIVATE FUNCTION: delay_ms
*
* PARAMETERS:
* ~ ui_value - The period for the delay in miliseconds.
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Delay in miliseconds.
*
*******************************************************************************/
void delay_ms(unsigned int ui_value)
{
while (ui_value-- > 0)
{
__delay_ms(1); // macro from HI-TECH compiler which will generate 1ms delay base on value of _XTAL_FREQ in system.h
}
}
void LE(unsigned int light) //led1-led6 function
{
LE1=light;
delay_ms(1000);
LE1=0;
LE2=light;
delay_ms(1000);
LE2=0;
LE3=light;
delay_ms(1000);
LE3=0;
LE4=light;
delay_ms(1000);
LE4=0;
LE5=light;
delay_ms(1000);
LE5=0;
LE6=light;
delay_ms(1000);
LE6=0;
}