8051直流電機(jī)的開環(huán)控制程序
#include
#include
#include
//-----------------------定義管腳--------------------------------------------------------
sbit PWM=P1^0; //PWM波形輸出
sbit DR=P1^1; //方向控制
#define timer_data (256-100) //定時(shí)器預(yù)置值,12M時(shí)鐘是,定時(shí)0.1ms
#define PWM_T 100 //定義PWM的周期T為10ms
unsigned char PWM_t; //PWM_t為脈沖寬度(0~100)時(shí)間為0~10ms
unsigned char PWM_count; //輸出PWM周期計(jì)數(shù)
unsigned char time_count; //定時(shí)計(jì)數(shù)
bit direction; //方向標(biāo)志為
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱:timer_init
// 函數(shù)功能:初始化設(shè)施定時(shí)器
//--------------------------------------------------------------------------------------------------
void timer_init()
{
TMOD=0x22; /*定時(shí)器1為工作模式2(8位自動(dòng)重裝),0為模式2(8位自動(dòng)重裝) */
PCON=0x00;
TF0=0;
TH0=timer_data; //保證定時(shí)時(shí)長(zhǎng)為0.1ms
TL0=TH0;
ET0=1;
TR0=1; //開始計(jì)數(shù)
EA=1; //中斷允許
}
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱:setting_PWM
// 函數(shù)功能:設(shè)置PWM的脈沖寬度和設(shè)定方向
//--------------------------------------------------------------------------------------------------
void setting_PWM()
{
if(PWM_count==0) //初始設(shè)置
{
PWM_t=20;
direction=1;
}
}
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱:IntTimer0
// 函數(shù)功能:定時(shí)器中斷處理程序
//--------------------------------------------------------------------------------------------------
void IntTimer0() interrupt 1
{
time_count++;
DR=direction;
if(time_count>=PWM_T)
{
time_count=0;
PWM_count++;
setting_PWM(); //每輸出一個(gè)PWM波調(diào)用一次
}
if(time_count
else
PWM=0;
}
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱:main
// 用戶主函數(shù)
// 函數(shù)功能:主函數(shù)
//--------------------------------------------------------------------------------------------------
void main()
{
timer_init();
setting_PWM();
}
admin2009-6-12 21:522#
//-----------------------函數(shù)聲明,變量定義--------------------------------------------------------
#include
#include
sbit SDA=P1^0; // 將p1.0口模擬數(shù)據(jù)口
sbit SCL=P1^1; // 將p1.1口模擬時(shí)鐘口
#define NUM 10 // 接收和發(fā)送緩存區(qū)的深度
#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};
unsigned char idata sendbuf[NUM]; // 數(shù)據(jù)發(fā)送緩沖區(qū)
unsigned char idata receivebuf[NUM]; // 數(shù)據(jù)接收緩沖區(qū)
bit bdata SystemError; // 從機(jī)錯(cuò)誤標(biāo)志位
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱: iic_start()
//函數(shù)功能: 啟動(dòng)I2C總線子程序
//--------------------------------------------------------------------------------------------------
void iic_start(void)
{ EA=0; //時(shí)鐘保持高,數(shù)據(jù)線從高到低一次跳變,I2C通信開始
SDA = 1;
SCL = 1;
delayNOP(); // 延時(shí)5us
SDA = 0;
delayNOP();
SCL = 0;
}
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱: iic_stop()
// 函數(shù)功能: 停止I2C總線數(shù)據(jù)傳送子程序
//--------------------------------------------------------------------------------------------------
void iic_stop(void)
{
SDA = 0; //時(shí)鐘保持高,數(shù)據(jù)線從低到高一次跳變,I2C通信停止
SCL = 1;
delayNOP();
SDA = 1;
delayNOP();
SCL = 0;
}
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱: slave_ACK
//函數(shù)功能: 從機(jī)發(fā)送應(yīng)答位子程序
//--------------------------------------------------------------------------------------------------
void slave_ACK(void)
{
SDA = 0;
SCL = 1;
delayNOP();
SDA = 1;
SCL = 0;
}
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱: slave_NOACK
// 函數(shù)功能: 從機(jī)發(fā)送非應(yīng)答位子程序,迫使數(shù)據(jù)傳輸過(guò)程結(jié)束
//--------------------------------------------------------------------------------------------------
void slave_NOACK(void)
{
SDA = 1;
SCL = 1;
delayNOP();
SDA = 0;
SCL = 0;
}
//--------------------------------------------------------------------------------------------------
// 函數(shù)名稱: check_ACK
//函數(shù)功能: 主機(jī)應(yīng)答位檢查子程序,迫使數(shù)據(jù)傳輸過(guò)程結(jié)束
//--------------------------------------------------------------------------------------------------
評(píng)論