標(biāo)題:
單片機(jī)2路電機(jī)轉(zhuǎn)向速控控制程序
[打印本頁]
作者:
暗黑暗夜
時(shí)間:
2023-11-27 00:39
標(biāo)題:
單片機(jī)2路電機(jī)轉(zhuǎn)向速控控制程序
#include<reg51.h>
#include<intrins.h> //因?yàn)橐玫阶笥乙坪瘮?shù),所以加入這個(gè)頭文件
//--定義使用的IO口--//
#define GPIO_IOA P0
//#define GPIO_IOB P1
//#define GPIO_IOC P2
//sbit P22=P2^2;
sbit P32=P3^2;
void InIT0_Config();
unsigned char Data_Value[5] ,Pwm_count;//單線數(shù)據(jù)
//=============================================延時(shí)函數(shù)================================================
void delay_ms(unsigned int ms)//誤差 0us
{
unsigned int a,b,c;
for(c=ms;c>0;c--)
for(a=15;a>0;a--);
}
//=============================================主函數(shù)================================================
void main(void)
{
int i;
InIT0_Config();//中斷初始化
while(1)
{
Pwm_count=Data_Value[1];
if(Pwm_count==0){
GPIO_IOA=0x00;
}else if(Pwm_count==255){
GPIO_IOA = ~Data_Value[0];
}else{
for(i=0;i<255;i++){
if(i<=Pwm_count){
GPIO_IOA = ~Data_Value[0];
}else{
GPIO_IOA=0x00;
}
delay_ms(3);
}
}
}
}
//=============================================中斷初始化函數(shù)================================================
void InIT0_Config(void)
{
IT0=1;//下降沿觸發(fā)
EX0=1;//打開中斷0允許
EA=1; //打開總中斷
P32=1;//初始化端口
P0=0xff;
P1=0xff;
P2=0xff;
}
//=============================================下降沿中斷執(zhí)行================================================
void INIT0(void) interrupt 0
{
unsigned char j,k;
unsigned int err_count;
err_count=0;
while((P32==0)&&(err_count<800)){//等待前面9ms的低電平過去
err_count++;
}
if((P32==1)&&(err_count>500)){//9ms低電平正確
err_count=0;
while((P32==1)&&(err_count<500)){ //等待4.5ms的起始高電平過去
err_count++;
}
if(P32==0){//4.5毫秒高電平正確
for(k=0;k<4;k++){ //共有4組數(shù)據(jù)
for(j=0;j<8;j++){ //接收一組數(shù)據(jù) 11110000
//=======================等待560us 開始
err_count=0;
while((P32==0)&&(err_count<100)){//等待560us的起始低電平過去
err_count++;
}
//=======================等待560us 結(jié)束
//=======================數(shù)據(jù)接收開始
if(P32==1){
err_count=0;
while((P32==1)&&(err_count<150)){ //計(jì)算高電平的時(shí)間長(zhǎng)度。
err_count++;
}
//=======================數(shù)據(jù)接收完成
//=======================保存數(shù)據(jù)開始
Data_Value[k]>>=1;
if(err_count>140){
EX0=1;
return;
}else if(err_count>40){
Data_Value[k]|=0x80;
}
//=======================保存數(shù)據(jù)完成
}
}
} //所有數(shù)據(jù)獲取完成
//===============執(zhí)行數(shù)據(jù)任務(wù)開始
//GPIO_IOA = ~Data_Value[0];
//GPIO_IOB = ~Data_Value[1];
//GPIO_IOC = ~Data_Value[2];
//===============執(zhí)行數(shù)據(jù)任務(wù)結(jié)束
}
}
}
復(fù)制代碼
作者:
75463339
時(shí)間:
2023-11-28 11:15
一個(gè)字都看不懂,但還是表示佩服。
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1