/********/
#pragma chip PIC18F1320 //CC8E compiler directive to use 18F1320.H
void Start_PWM (void);
void Stop_PWM (void);
/*********************************************************************************************/
// 1 MACRO DEFINITIONS AND CONSTANTS /
//***********************************`
//Logical Definitions:
//====================
//PIC18F1320 Definitions:
//=======================
#define F_OSC 8000000 //Frequency (Hz) of the oscillator (Fosc) for currently used clock
//ROBOT_PIC Definitions:
//======================
int distance_to_move;
int rotation_constant; // should be 1.754
int time_to_turn;
unsigned int counter;
#pragma origin 0x8 //Specify where in program memory to place the following code
//(0x8 corresponds to the High-Priority Interrupt Vector)
interrupt int_server(void) //The interrupt service routine is a special function. See CC8E User Manual for details
{
int_save_registers //CC8E Macro to save W, STATUS, and BSR registers
if (RBIF) //At least one of the RB4:RB7 is changed
{
if (!PORTB.5) // PB5 has received an interrupt
{
PORTB.2 = 1;
counter++;
}
RBIF = 0;
}
if (TMR2IF)
{
TMR2IF = 0;
}
int_restore_registers //CC8E Macro to restore W, STATUS, and BSR registers
}
/*********************************************************************************************/
// MAIN FUNCTION /
//*****************`
void main(void)
{ //Oscillator Initialization:
//==========================
SWDTEN = 0;
OSCCON = 0b01110000; // Configure for: !IDLEN, 8MHz INTOSC Frequency, PRI_RUN power mode
OSCTUNE = 0x10; //This register used to compensate for oscilator frequency drift error
//Port A,B Initialization:
//========================
ADON = 0;
ADCON1 = 0b01111111;
TRISA = 0b10000010;
TRISB = 0b00100000;
PORTA = 0x00; //Initialize Ports A,B to 0
PORTB = 0x00;
PORTA.6 = 0;
PORTB.6 = 0;
RBIE = 1;
RBIF = 0;
GIE = 1;
PEIE = 1;
Start_PWM();
while (1);
}
void Start_PWM (void)
{
CCP1CON = 0;
PR2 = 199;//249; //199 for DC=25%, 249 for DC=75%
CCPR1L = 49;//186; //49 for DC=25%, 186 for DC=75%
TRISB.3 = 0;
T2CON = 0x01;
CCP1CON = 0x3C;
TMR2 = 0;
TMR2ON = 1;
TMR2IE = 1;
}
void Stop_PWM (void)
{
TMR2ON = 0;
}
//END OF FILE