📄 t1.c
字号:
#include <p18f4520.h>
#include <delays.h>
#include <timers.h>
#include <pwm.h>
void Motor_ISR(void);
void low_isr(void);
void high_isr(void);
void set_motor(unsigned char, unsigned char);
void Delay1(void) ;
//**********define pins for motor**********//
#define MF PORTDbits.RD2
#define MB PORTDbits.RD3
//**********define motor variable**********//
#define motor_period 80
#define motor_forward() MF=1,MB=1
#define motor_stop() MF=0,MB=0
//**********define switch***************//
#define Switch_S1 PORTCbits.RC0
#define Switch_S2 PORTCbits.RC1
#define Switch_S3 PORTCbits.RC2
#define Switch_S4 PORTCbits.RC3
#define Switch_S5 PORTDbits.RD0
#define Switch_S6 PORTDbits.RD1
#define Switch_S7 PORTAbits.RA5
#define Switch_S8 PORTEbits.RE0
#define Switch_S9 PORTEbits.RE1
//**********define output pins **********//
#define F_LED PORTAbits.RA1
#define BR_LED PORTAbits.RA2
#define BL_LED PORTAbits.RA3
#define POWER_LED PORTAbits.RA4
#define BUZZER PORTEbits.RE2
//**********define global variable*******//
enum mode{FORWARD=0,BACKWARD};
unsigned char motor_speed=0, motor_count=0, motor_mode=FORWARD;
//**************Config*******************//
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config BOREN = OFF
/*************** Declare Interrupt Vector Sections *****************/
#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
_asm goto high_isr _endasm
}
#pragma code low_vector=0x18
void interrupt_at_low_vector(void)
{
_asm goto low_isr _endasm
}
/***************************** Declarations *************************/
#pragma code
/***************************** Set the Timer0 config *************************/
void T0_Config(void)
{
T0CON = 0b10001000; // Config Timer0 to Synchrounous counter mode, prescaler = 1:1, and Run Timer1 (TMR1ON = 1)
INTCONbits.TMR0IE = 1; //### Enable TIMER0 interrupts
INTCONbits.PEIE = 1; //### Enable Peripheral interrupts
INTCONbits.GIE = 1; //### Enable Global Interrups
INTCONbits.TMR0IF = 0; //### Clear Timer0 overflow flag
INTCON2bits.TMR0IP = 1;
TMR0H = 0xFF; //### Preload Timer0 register with calculated value
TMR0L = 0x9B; // In this case Timer0 (in counter mode) will overflow after 1 sec.
}
/***************************** Main Program *************************/
void main(void)
{
ADCON1=0x0f;
TRISA = 0b00100000;
TRISD = 0b00000000;
PORTA = 0b00000000;
PORTD = 0b00000000;
T0_Config();
while(1)
{
//----------------set motor----------------//
if(!Switch_S7)
{
if (!Switch_S8)
{
set_motor(0,80);
}
else
{
set_motor(0,62);
}
if (!Switch_S9)
{
set_motor(0,50);
}
}
else
{
set_motor(0,0);
}
//-----------Set the Front LED--------------//
if(!Switch_S1)
{
F_LED = 1;
Delay1();
F_LED = 0;
Delay1();
}
else
{
F_LED = 0;
}
//-----------Set the Back LED--------------//
if(!Switch_S2)
{
BR_LED = 1;
Delay1();
BR_LED = 0;
Delay1();
}
else
{
BR_LED = 0;
}
if(!Switch_S3)
{
BL_LED = 1;
Delay1();
BL_LED = 0;
Delay1();
}
else
{
BL_LED = 0;
}
//-----------Start the Buzzer --------------//
if(!Switch_S4)
{
//while(!Switch_S4);
BUZZER = 0;
Delay1();
BUZZER = 1;
Delay1();
}
/* //-----------Start the Timer --------------//
if(!Switch_S6) // Check if S2 is dipressed (RA4 == 0?)
{
while(!Switch_S6);
}
*/
}
}
#pragma interruptlow low_isr
void low_isr(void)
{
}
#pragma interrupt high_isr
void high_isr(void)
{
if (INTCONbits.TMR0IF)
{
INTCONbits.TMR0IF = 0;
TMR0H = 0xFF;
TMR0L = 0x9B;
Motor_ISR();
}
}
void Motor_ISR(void)
{
if (motor_count < motor_period)
motor_count++;
else
motor_count = 0;
if (motor_count < motor_speed)
{
if (motor_mode == FORWARD)
motor_forward();
}
else
motor_stop();
MB = MF;
}
void set_motor(unsigned char mode, unsigned char speed)
{
motor_mode = mode;
motor_speed = speed;
}
void Delay1(void)
{
int i;
Nop();
for(i=0;i<10000;i++)
{
Nop();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -