C source program of electric bicycle controlled by PIC16F72 microcontroller

Publisher:快乐时刻Latest update time:2016-03-25 Source: eefocusKeywords:PIC16F72 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include
//Electric vehicle dual closed-loop program, using dual closed-loop method to control the motor to obtain 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
//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 //State acquisition 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 //Maximum output of current loop
#define FULLDUTY 0X0FF //High level time when duty cycle is 1
#define SPEA 0X1d //Sum of 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, i.e.
//2.2 V
#define VOLON 0X4c //Low voltage protection restart voltage 3.0 V i.e. 33 V
#define VOLOFF 0X49 //Low voltage protection shutdown voltage 2.86 V i.e. 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, turn off all interrupts
TRISA=0XCF; //Set RA4,RA5 output
TRISB=0XEF; //RB port high three-bit input, collect the three-phase Hall signal of the motor
PORTC=new[(PORTB&AND)>>5]; //Collect the first Hall signal, and output the corresponding signal, turn on
//two MOS tubes
T2CON=0X01; //TMR2 4-division
CCPR1L=0X0FF; //Initial PWM output is all high
CCP1CON=0X0FF; //CCP1 is set to PWM mode
CCP2CON=0X0B; //CCP2 is set to special mode to trigger AD
ADCON0=0X81; //AD clock is 32 Frequency division, and AD is enabled, select AN0 channel to collect
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; //Current sampling period is set to TAD=512 μs
PR2=0XC7; //PWM frequency is set to 5 kHz
ADCON1=0X02; //AD result left shift
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 ​​counting 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 handle 256 times, battery voltage acquisition 1 time
ts=1; //Flag for acquisition of 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
}
//-----------State acquisition subroutine----------------------
void sample()
{
char state1,state2,state3,x;
do {
x=1;
state1=(PORTB&AND); //Hall signal acquisition
DELAY1(x);
state2=(PORTB&AND);
}while(state1-state2); //When the three sampling results are different, continue to collect stateif
(state1-oldstate!=0) //See if the sampling result is the same as the last time, if not
//then execute
{oldstate=state1; //Set this state to the old statestate1
=(oldstate>>5);
PORTC=new[state1]; //Port C outputs the corresponding signal to trigger the two MOS tubesif
(sp1==1){spe=1;sp1=0;}
else { //If the speed is very low, set spe to 1
spe=0;sp1=0;
speedcount<<=1;
state3=(TMR1H>>2); //Otherwise, spe=0, speed counter
speed=speedcount+state3; //speed register increases by 1 every 256 μs
}
speedcount=0;
}
}
//-----------------AD sampling subroutine----------------------
void AD()
{
char x;
ADIF=0; //Clear AD interrupt flag
if(ts==1){ //If it is handle sampling, then sample handle value
CHS0=1; //Select current sampling channel
count_vol=count_vol+1; //Battery sampling count register
spepid=1; //Set speed closed-loop operation flag
ts=0;tsh=ADRESH; //Store handle value
if(count_vol==0) { //If the battery sampling time is up, select AN2 channel to collect battery voltage
CHS0=0;CHS1=1;volflag=1;x=1;DELAY1(x);ADGO=1;
}
}
else if(volflag==1) { //Battery sampling is completed, and corresponding processing is performedCHS1
=0;CHS0=1;volflag=0;voltage=ADRESH;lowpower=1;
}
else { //Otherwise, the interrupt is a sampling current
interruptspeedcount=speedcount+1; //speedcount register plus 1, used as the speed measurementif
(speedcount>0x3d) sp1=1; //If the speed is lower than 1 000 000 μs/(512 μs*3eh*3)
//, it is considered to be in a low speed
statecurrenth=ADRESH;
curpid=1;
count_ts=count_ts-1;
if(count_ts==0) ​​{ //If the handle time is up, transfer to the handle sampling channelCHS0
=0;count_ts=0x08;ts=1;x=1;DELAY1(x);ADGO=1;
}
}
}
//-------------Brake processing subroutine------------------
void BREAKON()
{
char x;
off=0; //off clears, if it is interference, then do not reset
shutdown=0;
if(RB0==1) { //If the brake signal is true, then stop output voltage
ADIE=0; //Turn off AD interrupt
INTE=0; //Turn off brake interrupt
CCPR1L=FULLDUTY; //Output voltage 0
TMR1ON=0; //Turn off CCP2, no longer trigger AD
for(;ADGO==1;) continue;//If sampling is in progress, then wait for the end of sampling
ADIF=0; //ADIF bit clears
CHS0=0; //Select channel 0 sampling handle
CHS1=0;
x=1;
DELAY1(x);
do {
ADGO=1;
for(;ADIF==0;)continue;
ADIF=0;
CCPR1L=FULLDUTY;
asm("CLRWDT");
tsh=(ADRESH>>1);
}while(tsh>TSON||RB0==1); //When the handle value is greater than 2.2 V Or when the brake is still in progress, execute
the following statement:
off=1; //set reset flag
}
}
//---------Under-protection subroutine-------------------
void POWER()
{
char x;
lowpower=0;
voltage>>=1; //Convert the voltage value to 7 bits to facilitate single-byte operationif
(voltage ADIE=0;
INTE=0;
TMR1ON=0;
CCPR1L=FULLDUTY;
for(;ADGO==1;)continue;
ADIF=0;
CHS0=0;CHS1=1;
x=1;
DELAY1(x);
do{ADGO=1;
for(;ADIF==0;)continue;
ADIF=0;
voltage=(ADRESH>>1);
CCPR1L=FULLDUTY;
asm("CLRWDT");
}while(voltage =1; //Set reset flag
}
}
//------------Current loop operation subroutine-----------------
void CURPI()
{ static int curep=0x00,curek=0x00,curuk=0x00;
union data{int pwm;
char a[2];}b; //Define current loop operation register
curpid=0; //Clear current operation flag
curep=curek*CURB; //Calculate the product of the last deviation and the proportional coefficient
if(currenth<2)currenth=2; //If the sampled current is zero, it is considered that there is a small current to help
//reduce the speed
currenth>>=1;
curek=gcur-currenth; //Calculate this deviation
curuk=curuk+curek*CURA-curep; //According to the closed-loop PI operation method, get the output result this time,
//and process the result below
if(curuk<0x00) { //If the output is less than zero, it is considered that the output is zero
curuk=0;CCPR1L=FULLDUTY;CCP1X=0;CCP1Y=0;
}
else if(curuk-THL>=0) { //If the output is greater than the limit value, the maximum voltage is output
curuk=THL; CCPR1L=0; CCP1X=0; CCP1Y=0;
}
else { //Otherwise, output the corresponding high level time to CCPR1 register in proportion
b.pwm=THL-curuk;
b.pwm<<=1;
CCPR1L=ba[1]; //CCPR1L=(b.pwm>>8)&0x0ff; write the high nibble of PWM register
if(b.pwm&0x80!=0) CCP1X=1;
else CCP1X=0;
if(b.pwm&0x40!=0) CCP1Y=1;
else CCP1Y=0;
}
}
//---------------Speed ​​loop calculation subroutine-----------------------
void SPEPI()
{ static int speep=0x00,speek=0x00,speuk=0x00;
int tsh1,speed1; //Speed ​​register definition
spepid=0; //Clear speed operation flag
if(spe==1) speed1=0x00; //If the speed is too low, it is considered to be zero
else speed1=0x7f-speed; //Otherwise calculate the actual speed
if(speed1<0) speed1=0;
speep=speek*SPEB;
tsh1=tsh-0x38; //Get the handle value for calculation
speek=tsh1-speed1;
if(tsh1<0) {speuk=0;gcur=0;} //When the handle value is lower than 1.1 V, it is considered that the handle is zero
else { //Otherwise, calculate the corresponding speed loop output
if(tsh1>=GSPEH) //Limit the maximum speed
tsh1=GSPEH;
speuk=speuk+speek*SPEA-speep; //Calculate the speed loop output
if(speuk<=0X00) {speuk=0x00;gcur=0x00;}//speed loop output processingelse
if(speuk>GCURHILO) { //speed loop output limit, that is, limit the maximum current to about 12 A
speuk=GCURHILO;gcur=GCURH;}
else { //output in speed regulation
stategcur=(speuk>>4)&0x0ff;
}
}
}
//-----------Main program-------------------------
main()
{
for(;;){
INIT877(); //After the MCU is reset, initialize it firstoff
=0; //Clear reset flagfor
(;off==0;) { //If the reset flag is zero, execute the following program, otherwise reset
if(curpid==1) CURPI(); //current PI calculation
else if(spepid==1) SPEPI(); //speed PI calculation
else if(lowpower==1) POWER();
else if(shutdown==1) BREAKON();
asm("CLRWDT");
}
}
}
//---------interrupt service subroutine---------------------
#pragma interrupt_level 1
void interrupt INTS(void)
{
if(RBIF==1) {RBIF=0;sample();}
else if(ADIF==1) AD();
else if(INTF==1) {shutdown=1;INTF=0;} //brake interrupt, set brake flag


Design idea:
Purpose
Currently, there are a wide variety of brushless controllers with various functions in the electric vehicle market. Ordinary analog dedicated chips are at the end of their rope, while the use of single-chip microcomputer control can achieve the point of "only thinking about it, not afraid of not being able to do it". A variety of single-chip microcomputers have been introduced to the emerging industry of electric vehicles. According to the popular trend of the electric vehicle market, our company has developed a design plan for brushless controllers.
Functional Overview
At present, the controllers on the electric vehicle market are divided into two categories: brushed controllers and brushless controllers. Due to the many shortcomings of brushed motors, such as small output torque, low efficiency, and the need to regularly replace carbon brushes, they are gradually replaced by brushless motors with large output torque, high efficiency, and long service life. According to the model of electric vehicles, they are divided into simple cars, luxury models, and electric motorcycles. The power of simple cars is generally below 250W, while the power of luxury cars is above 350W, which must be considered during design. The common functions of simple cars are 1:1 power assistance, cruise, power and working status display.
There are two working modes: automatic and manual switching. Luxury models have many functions according to the randomness of customers, mainly including flying car protection, soft ABS brakes, reverse charging, dual power (gear switching), motor lock (motor lock when power is turned off), etc.
In order to facilitate debugging and prevent illegal decryption, the design uses a special debugging tool, and an external tool with a keyboard and display (digital tube) is used to set some basic parameters, such as undervoltage value, current limiting, phase selection and working voltage selection. The setting parameters can be saved in the internal or external EEPROM of the microcontroller. The debugging tool achieves the versatility of the series of products.

Main technical parameters
1 Basic functions
1.1 Working voltage
Keyboard setting, divided into 12, 24, 36, 48, 60, 72V gears, according to the input voltage sampling value, determine the undervoltage protection value, the single battery protection voltage is 10.5V ± 0.5V, and the output is turned off below this value. Since the sampling voltage has a corresponding error, it should be fine-tuned with the keyboard. Undervoltage working mode: When the power supply voltage is lower than the set value, the output is turned off, and when the power supply voltage hysteresis returns to a value greater than the set value of 2V, the output is turned on. Another way is that when the power supply voltage is lower than 50% of the battery capacity, the output pulse width is reduced accordingly, and it is gradually reduced by 10 percentage points. When it reaches the set value, it is reduced to zero, that is, the output is turned off, and the hysteresis is the opposite.   
1.2 Speed ​​Control
Voltage The speed control output voltage range is 1~4.2V, the controller starting voltage should be higher than 1V, the controller pulse width modulation range should be set to 1.38~3.8V, and the output is fully open when it is greater than 3.8V.
1.3 Brake Power Off
There are three modes: high level, low level and ABS. The high and low level control modes are set by the keyboard. ABS is controlled by a separate pin. If this function is not used, the I/O port can specify other functions.
1.4 Current Limitation
When the sampling signal reaches the set point, the PWM is reduced to reduce the motor current so that the output current does not exceed the set value. That is, the maximum output current is constant at the set point. The set value is set by the keyboard for debugging.
1.5 Overcurrent Protection:
Due to the sampling speed of the MCU A/D, the output current is greater than the set value. In this case, a protection value is set to turn off the output. It is generally set to be 2~3A greater than the current limit value. This value should be set by the keyboard.
1.6 Stall Protection
After the current limit value is maintained for 1~3 seconds, the output is turned off.
1.7 Phase angle selection
60 degrees/120 degrees selection, keyboard setting.
1.8   1:1 power assist
The input 3:2 duty ratio switch signal 1~5.5Hz corresponds to the voltage signal of the speed control handlebar of 2~3.8V. According to the change of input frequency, the duty ratio of the output PWM is changed to control the riding speed.
1.9 Cruise
The manual/automatic selection is set by the keyboard. The manual button is effective at low level. The button is pressed for 2 seconds to enter the manual cruise mode; the automatic cruise mode is that the speed control handlebar is constant at a certain point for 8S (the signal voltage must be greater than the starting voltage), and the controller automatically enters the cruise mode.
1. 10 Speed ​​limit
The method of reducing PWM pulse width is adopted. This value is fine-tuned by the keyboard, and the initial value is defined as 45% of the maximum PWM value. Low level is the speed limit mode.
1.11 Fault indication
Flash 1 normal, flash 2 brake, flash 3 RAO, RBO, flash 4 down drive, flash 5 up drive, flash 6 phase loss, flash 7 RBO, flash 8 undervoltage. The fault status indication is indicated by the indicator light of the dedicated debugger.
1.12 Speed-off protection
Speed ​​control voltage>4.5V, power-on speed control voltage>1.5V turns off output protection. That is, when the speed control handle ground wire is open and the speed control handle has been turned before the electric door lock is opened.
1.13 Reverse charging
Coasting charging, EBS brake charging, and coasting charging are selected using the I/O port. Low level is coasting charging. Output an indication signal. The indicator light is on for charging status.
2 Additional functions
2.1 Dynamic display
1. Fault display 2. Battery and riding status display 3. Speed ​​display (LED)
2.2 Dual power
Set a switching point according to the motor speed. The value of the switching point is set by the keyboard.
2.3     Gear switching
The speed of the three gears is set by a button switch. The initial state is the lowest speed. The working mode of the button is to press the button switch and enter the gear state after releasing it. The gear is in a cyclic mode. The gear speed can be fine-tuned by the keyboard.
2.4     Pointer instrument
Speed ​​split phase line output, Hall signal, and single-chip microcomputer output.
2.5     Anti-theft lock
Input a signal to lock the motor. The faster you push, the greater the resistance (this function can be made into a motor lock, which is realized after the electric lock is turned off).
2.6     Parameter setting
The display window consists of two parts. The first part is the function number, and the second part is the parameter value. The keypad consists of three buttons representing mode, plus, and minus. The set parameters are saved in the EEPROM memory. The communication between the setter and the microcontroller adopts I2 C method.
Keywords:PIC16F72 Reference address:C source program of electric bicycle controlled by PIC16F72 microcontroller

Previous article:Design of DC motor servo system based on PWM module and CWG module
Next article:Introduction to PIC microcontroller C language programming (4)

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号