常驻嘉宾
- 积分
- 3834
- 威望
- 1156
- 贡献
- 2216
- 兑换币
- 11
- 注册时间
- 2010-9-26
- 在线时间
- 231 小时
|
本帖最后由 王其奇 于 2010-11-12 21:35 编辑
#include <hidef.h> /* common defines and macros *///有的注释是前期的了,会有一些错误,但是还是能明白的
#include <MC9S12XS128.h> /* derivative information */
#include "derivative.h" /* derivative-specific definitions */
#define uchar unsigned char
#define uint unsigned int
#define SCE PTT_PTT0
#define RST PTT_PTT1
#define DC PTT_PTT2
#define DIN PTT_PTT3
#define CLK PTT_PTT4
#define LED PTT_PTT5
//字库太长就删了
unsigned char Ton[10]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
};
void pwm_init(void) {
PWMPRCLK=0x44; //通道时钟预分频, 16分频
PWMSCLA=0x09; //时钟A分频 , 50分频
PWMSCLB=0x19; //时钟B分频 , 50分频
PWMCLK=0xFF; //PCLK1选择ClockSA,PCLK选择ClockSB
PWMPOL=0xFF; //起始输出高电平
PWMCAE=0x00; //PWM输出左对齐
PWMCTL=0xF0; //通8道01级联,通道23级联
PWMPER0=0x0A;
PWMPER1=0X9DA;
PWMPER2=0x00; //级联通道23的周期高字节
PWMPER3=0x09; //级联通道23的周期低字节
PWMPER4=0x00;
PWMPER5=0x09;
PWMPER6=0x0A;
PWMPER7=0x9DA;
PWMDTY0=0x00; //级联通道01的占空比高字节
PWMDTY1=200; //级联通道01的占空比低字节
PWMDTY2=0x00; //级联通道01的占空比高字节
PWMDTY3=Ton[0]; //级联通道01的占空比低字节
PWMDTY4=0x00;
PWMDTY5=Ton[0];
PWMDTY6=0x00;
PWMDTY7=0xB0;
PWME=0xFF;
}
//锁相环寄存器初始化
void Pll_Init(void)
{
REFDV = 129; // 设置REFDV寄存器
SYNR =196; // 选择时钟频率,在.h里选SYNR_SET的值
asm nop // 等待PLL 稳定
asm nop
asm nop
asm nop
while ((CRGFLG&0x08) == 0); // 等待PLL频率锁定
CLKSEL |= 0x80; // PLL向系统总线输出时钟
}
void delay_1ms(void)//1ms 延时函数
{
uint i;
for (i=0;i<600;i++);
}
void delay_nms(uint n) //N ms 延时函数
{
uint i=0;
for (i=0;i<n;i++)
delay_1ms();
}
void LCD_write_byte(unsigned char dt, unsigned char command)
{
unsigned char i;
SCE=0;
DC=command;
for(i=0;i<8;i++)
{
if(dt&0x80)
DIN=1;
else
DIN=0;
dt=dt<<1;
CLK=0;
CLK=1;
}
DC=1;
SCE=1;
DIN=1;
}
void LCD_init(void)
{
DDRT=0Xff;
RST=0;
delay_1ms();
RST=1;
LCD_write_byte(0x21,0);//初始化 Lcd,功能设定使用扩充指令 LCD_write_byte(0xd0,0);//设定液晶偏置电压
LCD_write_byte(0xd0,0);
LCD_write_byte(0x20,0);//使用基本指令
LCD_write_byte(0x0C,0);//设定显示模式,正常显示
}
void LCD_set_XY(unsigned char X, unsigned char Y)
{
LCD_write_byte(0x40 | Y, 0);// column
LCD_write_byte(0x80 | X, 0);// row
}
void LCD_clear(void)
{
unsigned char t;
unsigned char k;
LCD_set_XY(0,0);
for(t=0;t<6;t++)
{
for(k=0;k<84;k++)
{
LCD_write_byte(0x00,1);
}
}
}
void LCD_write_shu(unsigned char row, unsigned char page,unsigned char c) //row:列 page:页
//dd:字符
{
unsigned char i;
LCD_set_XY(row*4, page);// 列,页
for(i=0; i<8;i++)
{
LCD_write_byte(shuzi[c*16+i],1);
}
LCD_set_XY(row*4, page+1);// 列,页
for(i=8; i<16;i++)
{
LCD_write_byte(shuzi[c*16+i],1);
}
}
void main(void)
{
uint a,c,i,j,k,l,m,dj,kz;
l=3;
kz=0;
Pll_Init();
pwm_init();
LCD_init(); //初始化 LCD 模块
LCD_clear(); //清屏幕
DDRT=0xFF;
i=0,j=0,m=0;
LED=1;
delay_nms(300);
LED=0;
delay_nms(1000);
LED=1;
EnableInterrupts;
//PWMDTY1=0xC5;
delay_nms(500);
PWMDTY7=0xB0;
delay_nms(500);
for(dj=195;dj<196;dj++){
PWMDTY1=197;
//if(kz%2==0)
delay_nms(10000); //舵机从173到223,居中为197(跑试)
PWMDTY3=Ton[3];
// else
// PWMDTY5=Ton[3];
for(k=0;k<1;k++){
a=PWMDTY1/100;
c=(PWMDTY1-a*100)/10;
i=PWMDTY1-a*100-c*10;
LCD_write_shu(j,0,13); //舵机速度显示
j+=2;
LCD_write_shu(j,0,28);
j+=2;
LCD_write_shu(j,0,50);
j+=2;
LCD_write_shu(j,0,a);
delay_nms(500);
j+=2;
LCD_write_shu(j,0,c);
delay_nms(500);
j+=2;
LCD_write_shu(j,0,i);
delay_nms(500);
j=0; //
LCD_write_shu(m,2,22); //MOTOR速度显示
m+=2;
LCD_write_shu(m,2,28);
m+=2;
LCD_write_shu(m,2,50);
m+=2;
LCD_write_shu(m,2,3);
m+=2;
delay_nms(500);
LCD_write_shu(m,2,13);
delay_nms(500); //
m=0;
}
delay_nms(20000);
PWMDTY3=Ton[0];
PWMDTY1=215;
delay_nms(500);
PWMDTY3=Ton[4];
delay_nms(5000);
PWMDTY3=Ton[0];
PWMDTY5=Ton[3];
delay_nms(2000);
PWMDTY5=Ton[0] ;
PWMDTY1=197;
PWMDTY3=Ton[3];
delay_nms(20000);
PWMDTY3=Ton[0];
}
} |
|