Programming the electric bicycle drive system controlled by a single-chip microcomputer using C language
[Copy link]
#include <pic.h> // Electric vehicle dual closed loop program, using dual closed loop method to control the motor to get the best zh speed performance, and can // Limit the maximum current of the motor. This application uses two CCP components, CCP1 is used for PWM output to control // Control motor voltage; CCP2 is used to trigger AD , timer TMR2 , TMR1 , INT interrupt, RB port level change interrupt, // Watchdog and 6 general I/O ports #define AND 0xe0 // Status collection 5 , 6 , 7 bits #define CURA 0X0a // Sum of current loop proportional and integral coefficients #define CURB 0X09 // Current loop proportional coefficient #define THL 0X6400 // current loop maximum output #define FULLDUTY 0X0FF // High level time when duty cycle is 1 #define SPEA 0X1d // Sum of the speed loop proportional and integral coefficients #define SPEB 0X1c // Speed loop proportional coefficient #define GCURHILO 0X0330 // Maximum output of speed loop #define GCURH 0X33 // Maximum given current #define GSPEH 0X67 // Maximum speed setting #define TSON 0X38 // handle opening voltage 1.1 V , TSON*2 is the handle opening voltage after braking, that is //2.2 V #define VOLON 0X4c // Low voltage protection reset voltage 3.0 V or 33 V #define VOLOFF 0X49 // Low voltage protection shutdown voltage 2.86 V or 31.5 V volatile unsigned char DELAYH,DELAYL,oldstate,speed, speedcount,tsh,count_ts,count_vol,gcur,currenth, voltage; // register definition static bit sp1,spe,ts,volflag,spepid,lowpower, off,shutdown,curpid; // flag definition static volatile unsigned char new[10]={0xaf,0xbe,0xff,0x7e,0xcf, 0xff,0xd7,0x77,0xff,0xff}; // Status register table //------------PIC16F877 Initialization subroutine ------------ void INIT877() { PORTC=0X0FF; // Turn off all MOSFETs TRISC=0X02; // Set port C output PIE1=0X00; // Initialize interrupt register and turn off all interrupts TRISA=0XCF; // Set RA4, RA5 output TRISB=0XEF; //RB port high three-bit input, collect the Hall signal of the three-phase motor PORTC=new[(PORTB&AND)>>5]; // Collect the first Hall signal and output the corresponding signal to turn on // Two MOS tubes T2CON=0X01; //TMR2 4- division CCPR1L=0X0FF; // Initially PWM output is full high CCP1CON=0X0FF; //CCP1 is set to PWM mode CCP2CON=0X0B; //CCP2 is set to a special mode to trigger AD ADCON0=0X81; //AD clock is divided by 32 , and AD is enabled , select AN0 channel acquisition // Handle voltage TMR2=0X00; //TMR2 register initialization TMR1H=0X00; //TMR1 register initialization TMR1L=0X00; T1CON=0X00; //TMR1 is divided by 1 CCPR2H=0X08; CCPR2L=0X00; // The current sampling period is set to TAD = 512 μs PR2=0XC7; //PWM frequency is set to 5 kHz ADCON1=0X02; //AD result shift left OPTION=0XFB; //INT rising edge trigger TMR2ON=1; //PWM starts working INTCON=0XD8; // Interrupt setting GIE=1, PEIE=1, RBIE=1 ADIE=1; //AD interrupt enable speedcount=0x00; // speed count register speed=0x7f; // Speed holding register spe=1; // Low speed flag sp1=1; // Low speed flag oldstate=0x0ff; // Initial state setting, different from other states count_ts=0x08; // Current sampling 8 times , handle acquisition 1 time count_vol=0x00; // sample the handle 256 times and collect the battery voltage once ts=1; // The flag that can collect the handle value ADGO=1; //AD sampling enable TMR1ON=1; //CCP2 component starts working } //------------ Delay subroutine --------------- #pragma interrupt_level 1 void DELAY1(x) char x; { DELAYH=x; // delay parameter setting #asm DELAY2 MOVLW 0X06 MOVWF _DELAYL DELAY1 DECFSZ _DELAYL GOTO DELAY1 DECFSZ _DELAYH GOTO DELAY2 #endasm }
|