//**********************Include Library
FILES*******************************//
#include<p18f67k22.h>
#include<stdio.h>
//**********************Variable Declaration*******************************//
unsigned int currentStatus=0,previousStatus=0; //initially
define as Stopped
unsigned long decimalNum=0;
unsigned int isr_us=10000; //ISR for 10ms
unsigned int crystal_freq_MHZ=20;
unsigned int presclar_value=1;
unsigned long IsrDecimalValue=0,quotient;
unsigned int remainder;
unsigned int low_v,high_v,speed;
//*********************************** function
definition*****************************************//
void timer1_init(void)
{
INTCONbits.GIEH = 1; //Set Global Interrupt
Enable
INTCONbits.GIEL = 1; //Set Global Interrupt
Enable
T1CONbits.RD16=0;
T1CONbits.NOT_T1SYNC=0;
T1CONbits.SOSCEN=0;
T1CONbits.T1CKPS0=0;
T1CONbits.T1CKPS1=0;
T1CONbits.TMR1CS0=0;
T1CONbits.TMR1CS1=0;
PIE1bits.TMR1IE=1; //TIMER1 OVERFLOW INTERRUPT
ENABLE
PIR1bits.TMR1IF=0; //TIMER1 OVERFLOW INTERRUPT FLAG
BIT
isr_calulation();
TMR1L=low_v;
TMR1H=high_v;
T1CONbits.TMR1ON=1;
}
void PWM_init()
{
TRISAbits.TRISA2=1;
PWM3CON=0;
PR2=255;
PWM3DCH=0x00;
PWM3DCL=0x00;
PIR1bits.TMR2IF=0;
T2CON=0x03;
PWM3DCL=0;
T2CONbits.TMR2ON=1;
PWM3CONbits.PWM3EN=1;
TRISAbits.TRISA2=0;
PWM3CONbits.PWM3OE=1;
PWM3CON=0xF0;
}
void ADC_Init(unsigned char chanel)//to convert digital channel to
analog
{
num=chanel;
ADCON0=(num1|(num<<2));//num1=0000 0001 or
num=channel
ADCON1 = 0xF0;//1 = Right justified;111 = FRC (clock
supplied from a dedicated RC oscillator);Unimplemented: Read as
‘0’;00 = VREF+ is connected to VDD
}
void Readadc(void)//read adc channel
{
ADCON0bits.GO=1; //1 = A/D conversion cycle in
progress
while(ADCON0bits.GO==1); // Wait for completion
//ConvertADC(); // Start conversion
//while(BusyADC()); // Wait for completion
a = ADRESH; //ADC RESULT REGISTER HIGH bits;Upper 2
bits of 10-bit conversion result
b = ADRESL; //ADC RESULT REGISTER low bits;Lower 8
bits of 10-bit conversion result
//****combine high and low adc value of 10 bit********//
thld = a;
thld = thld<<8;
thld = thld | b;
}
void isr_calulation() //isr for 10 ms
{
decimalNum = isr_us *
((crystal_freq_MHZ/4)*(1/presclar_value));
IsrDecimalValue = 65535 - decimalNum;
low_v = IsrDecimalValue & 0xff;
IsrDecimalValue = IsrDecimalValue/256;
high_v = IsrDecimalValue & 0xff;
}
/*********************ISR*********************/
#pragma code high_vector=0x08
#pragma interrupt high_isr
void interrupt_at_high_vector(void)
{
_asm GOTO high_isr _endasm
}
#pragma code /* return to the default code section */
void high_isr(void)
{
if (PIR1bits.TMR1IF == 1) //Check Timer 1 Overflow
Interrupt Flag To make sure this is a Timer 1 interrupt.
{ //Peripheral Interrupt Request Flag
registers
PIR1bits.TMR1IF=0;
TMR1L=low_v;
TMR1H=high_v;
--count;
Read_Sensor(); //read Sensor data
and update
}
}
//*******main initialization*****//////
void main()
{
//CLEAR ALL PORT
PORTA=0x00;
PORTB=0x00;
PORTC=0x00;
PORTD=0x00;
//CONTROLLER REGISTER SET AND RESET ACORDING TO
NEED
CM1CON=0x00;//comparator disable
CM2CON=0x00;
CM3CON=0x00;
//CLEAR ALL LAT
LATA=0X00;
LATB=0X00;
LATC=0X00;
LATD=0X00;
//DEFINE ANALOG TO DIGITAL
ADCON0=0x01; //channel 0 define as analog
ADCON1=0x00;
ADCON2=0x00;
ANCON0=0X01;
ANCON1=0X00;
ANCON2=0X00;
//DEFINE DIRECTION AS INPUT AND OUTPUT
TRISA=0xFF; //define as input
TRISB=0xFF;
TRISC=0X00; //define as output
TRISD=0X00;
//FUNCTION INITIALIZATION
timer1_init();//timer initilization
PWM_init(); //PWM Init
while(1)
{
ADC_Init(0); //init channel 0
Readadc(); //read channel 0s
PWM3DCH=speed; //motor control
according to speed,Pwm pin connected to motor1 and motor2
}
}
. It is desired to write an interrupt driven software for the robot. Two global variables namely, currentStatus and previous Status should be declared that represent the current and previous stat...