 |
:: استاذ و مشرف قسم الالكترونيات ::
تاريخ التسجيل: May 2007
المشاركات: 6,894
|
|
نشاط [ F.Abdelaziz ]
قوة السمعة:332
|
|
11-02-2012, 09:47 PM
المشاركة 7
|
|
كود البرنامج :
كود:
//STEP 1 Comments
/*Name : LED & SOUNDER
PICMICRO : PIC16F877A */
// STEP 2: definitions
#define TEMP_LED 250 // Constant value , 250 ,defined as "TEMP_LED .
#define LED PORTD.B0 //LED at pin RD0=PORTD.B0 defined as "LED" '
#define BUZZER PORTD.B1 //SOUNDER at pin RD1 = PORTD.B1 defined as "BUZZER" .
#define T0IF INTCON.T0IF//Timer0 Interrupt Flag Bit INTCON.F0IF defined as "T0IF"
// STEP 3 : Variable decleraion
unsigned char TimerLed = 0 ;
/* Variable : Char data type is an 8-bit,
so it can get to hold values up to 255,
we need a maximum of 250,TEMP_LED , so it's fine */
//STEP 4 : Setting Function , Others
void settings ( void ) // Setting Function
{
//PORTS Setting
TRISD = 0 ;// PORTD as outputs
// Setting the OPTION register
OPTION_reg = 0b11000100 ;
// bit 0 -> Prescaler Rate Select bit 0 >0
// bit 1 -> Prescaler Rate Select bit 0 >0
// bit 2 -> Prescaler Rate Select bit 0 >1 (3 bits 100= 1:32)
// bit 3 -> Prescaler assigned to Timer0 >0
// bit 4 -> It does not matter >0
// bit 5 -> Timer0 Clock derived from internal clock cycle >0
// bit 6 -> It does not matter >1
// bit 7 -> pull-up resistors on port B disabled >1
// Set interrupt
INTCON = 0b10100000 ;
// bit 0 -> RBIF - Flag of interrupt ports B >0
// bit 1 -> INTF - Interrupt Flag of RB0/INT >0
// bit 2 -> T0IF - on Timer0 interrupt flag >0
// bit 3 -> RBIE, Interrupt on Port B disabled >0
// bit 4 -> INTE, Interrupt port RB0/INT disabled >0
// bit 5 -> TMR0IE, Interrupt on Timer0 enabled >1
// bit 6 -> PEIE, interrupt device disabled >0
// bit 7 -> GIE, Management Interrupt active >1
TMR0 = 100 ; // Set Timer0 to 100 , as initial value
}
//step 5 : main function
void main ( )
{
settings ( ) ; // Run Setting Function for ports and registers
while ( 1 ) // run a loop
{
/* The only thing that I do during this endless cycle,
is the reversal of the state of BUZZER each 200 micro-sec
in order to generate a square wave of 2.5 KHz,
which applied at the buzzer,
in fact, causing it to emit a note at that frequency */
Delay_us ( 200 ) ;
BUZZER = 1 ;
Delay_us ( 200 ) ;
BUZZER = 0 ;
} // end loop
} // end main
// STEP 6 : Interrupt Routine Service IRS
/* This routine, having the attribute "interrupt"
which called automatically whenever an interrupt occurs.
in this routine , Timer0 will generate an interrupt every millisecond. */
void interrupt ( )
{
if( T0IF ) // Interrupt was caused by an overflow of Timer0?
{
TMR0 = 100 ; // Timer0 reloaded
TimerLed ++; // Increment the timer for the blinking LED
if( TimerLed >= TEMP_LED ) // If the time has passed TEMP_LED=250
{
LED = LED^1 ; // Invert LED status to flash .
TimerLed = 0 ; // Reloading the LED timer to start again
}
T0IF = 0 ; // Reset the interrupt flag of timer 0
} // end that occurred on Timer0 interrupt
} // end of interrupt service routine
|