‘壹’ 用c语言写一个单片机控制小车走直线的程序,小车一共3个轮子,一个万向轮,另两个是电机驱动的,用51单片
自己看注释改一下就行 函数都写好了了 没用的删掉就可以了
#include<reg52.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
#define L_M 1
#define R_M 2
uchar temp2,temp1,t,m1,m2,redline;
sbit tr=P2^4; //超声波
sbit ec=P2^5; //
sbit en=P2^7; //液晶的EN
sbit rs=P2^6; //液晶RS选择端
//液晶数据口 P0口
sbit Rin_1=P2^0; //寻迹管脚输入
sbit Rin_2=P2^1;
sbit Lin_2=P2^2;
sbit Lin_1=P2^3;
sbit L_EN=P1^0; //左侧电机使能
sbit L_1=P1^1; //左侧电机控制 1
sbit L_2=P1^2; //左侧电机控制 2
sbit R_1=P1^3; //右侧电机控制 1
sbit R_2=P1^4; //右侧电机控制 2
sbit R_EN=P1^5; //右侧电机使能
void delay(uint z) //延时程序1
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delay1s(uchar t) //1s延时函数
{
uchar a,h,i,j,k;
for(a=t;a>0;a--)
{
for(h=5;h>0;h--)
for(i=4;i>0;i--)
for(j=116;j>0;j--)
for(k=214;k>0;k--);
}
}
void v_cotrol(uchar number,uchar speed)
{
if(speed<100)
{
if(number==1)
m1=abs(speed);
if(number==2)
m2=abs(speed);
}
}
void turnleft()
{
L_1=1; // left fan zhuan //
L_2=0;
R_1=1; // right fanzhuan
R_2=0;
}
void turnright()
{
L_1=0; // left zheng zhuan
L_2=1;
R_1=0; // right fanzhuan
R_2=1;
}
void gostright()
{
L_1=0; // left zheng zhuan
L_2=1;
R_1=1; // right zhengzhuan
R_2=0;
}
void goback()
{
L_1=1; // left zheng zhuan
L_2=0;
R_1=0; // right zhengzhuan
R_2=1;
}
void stop()
{
L_1=1; // stop
L_2=1;
R_1=1; // stop
R_2=1;
}
void timer0_int()
{
TMOD=0X01;
TH0=-100/256;
TL0=-100%256;
EA=1;
ET0=1;
TR0=1;
}
state_int()
{
Rin_1=1;
Rin_1=1;
Lin_1=1;
Lin_1=1;
t=0;
m1=50;
m2=50;
stop();
}
void main()
{
timer0_int();
state_int();
while(1)
{
//避障程序
if((Lin_1==1)&&(Lin_2==1)&&(Rin_1==1)&&(Rin_2==1))
gostright();
if((Lin_1==1)&&(Lin_2==1)&&(((Rin_1==1)&&(Rin_2==0))||((Rin_1==0)&&(Rin_2==1))||((Rin_1==0)&&(Rin_2==0))))
turnleft();
if((Rin_1==1)&&(Rin_2==1)&&(((Lin_1==1)&&(Lin_2==0))||((Lin_1==0)&&(Lin_2==1))||((Lin_1==0)&&(Lin_2==0))))
turnright();
if((((Lin_1==1)&&(Lin_2==0))||((Lin_1==0)&&(Lin_2==1))||((Lin_1==0)&&(Lin_2==0)))&&(((Rin_1==1)&&(Rin_2==0))||((Rin_1==0)&&(Rin_2==1))||((Rin_1==0)&&(Rin_2==0))))
goback();
if((Lin_1==1)&&(Lin_2==1)&&(Rin_1==1)&&(Rin_2==1))
gostright();
if((Lin_1==1)&&(Lin_2==1)&&(((Rin_1==1)&&(Rin_2==0))||((Rin_1==0)&&(Rin_2==1))||((Rin_1==0)&&(Rin_2==0))))
turnleft();
if((Rin_1==1)&&(Rin_2==1)&&(((Lin_1==1)&&(Lin_2==0))||((Lin_1==0)&&(Lin_2==1))||((Lin_1==0)&&(Lin_2==0))))
turnright();
if((((Lin_1==1)&&(Lin_2==0))||((Lin_1==0)&&(Lin_2==1))||((Lin_1==0)&&(Lin_2==0)))&&(((Rin_1==1)&&(Rin_2==0))||((Rin_1==0)&&(Rin_2==1))||((Rin_1==0)&&(Rin_2==0))))
goback();
}
while(1);
}
timer0() interrupt 1
{
TH0=-100/256;
TL0=-100%256;
if(t==0)
{
temp1=m1;
temp2=m2;
}
if(t==temp1)
L_EN=0;
if(t==temp2)
R_EN=0;
t++;
if(t==50)
{
t=0;
L_EN=1;
R_EN=1;
}
}
‘贰’ C语言如何编程,可以接收用USB连接的设备上扫描到的信息(USB设备驱动已安装)
这个一般涉及到文件读取的知识。打开课本最后一章,文件,里面有详细的介绍,不过仅限于读取某个文件,就像C:\Windows\ABLKSR这样子读取,具体实现是
#include "stdio.h"
void main()
{
FILE *fp1;
fp=fopen(“C:\Windows\ABLKSR”,r+);
free fp1;
//如果想要输出的话用printf,想进一步了解的话可以继续追问
}
最具体实现的话就要用MFC 或WINAPI了,可以实现文件查看,打开关闭修改,等等,不过涉及相对较深,单纯用C的简单语句的实现可能要编上1年
‘叁’ 步进电机驱动程序C语言
步进电机控制程序(c语言+51单片机)
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
#define ms *77
// f = 12 M
#define LEDLen 4
#define Dj_star() {IE=0x81; pri_dj=0; }
#define Dj_stop() {IE=0x00; pri_dj=1; P1=0xff; shache="0"; delay(800ms); delay(800ms);delay(400ms); shache = 1; }
#define Chilun_Num 8
/* 齿轮数 8 个*/
#define set_display_num() { LEDBuf[0] = tmp / 1000; LEDBuf[1] = tmp / 100 % 10; \
LEDBuf[2] = tmp / 10 % 10; LEDBuf[3] = tmp % 10; }
uchar LEDBuf[LEDLen] = {0,0,0,0};
void read_num (); /* 读播码盘 到 set_round_num * 8 */
void display ();
void delay(uint delay_time) { uint i; for (i=0; i < delay_time ; i++) ; }
void run ();
void fx_run();
uint round_num = 0; /* 记录已转的 齿轮数 , 中断1次 加 1*/
uint set_round_num = 0; /* 播码盘设置 圈数 */
uint set_pwm_width = 0; /* 播码盘设置 步进电机 正向速度 */
bit one_round_flg = 0;
sbit led_1000 = P0^7; //use for display
sbit led_100 = P0^6; //use for display
sbit led_10 = P0^5; //use for display
sbit led_1 = P0^4; //use for display
sbit key_start = P3^0;
sbit key_puse = P3^0;
sbit key_clear = P3^1;
/* P3^2 接齿轮传感器 中断 */
sbit bujin_zx_stop = P3^3; /* 接步进电机 ,正向到位传感器 ,为 0 停机 */
sbit bujin_fx_stop = P3^4; /* 接步进电机 ,反向到位传感器 ,为 0 停机 */
sbit shache = P3^5; /* 接刹车控制继电器 0 电位有效 */
sbit pri_dj = P3^6; /* 接主电机控制继电器 0 电位有效 */
void main(){
TCON = 0x01;
display();
while(1) {
IE="0x00";
round_num = 0;
display();
if ( bujin_fx_stop ) fx_run();
while ( key_start );
delay ( 8ms );
if(!key_start){
read_num();
//set_round_num = 8;
while ( !key_start );
run ();
fx_run();
}
}
}
void run () {
#define Delay_time 180
/* 转一圈 50 次循环,每循环 4 步 ,50 * 4 = 200 , 200 * 1。8 = 360 */
uchar i ;
P1 = 0xff;
set_pwm_width = 15 + set_pwm_width / 10;
while ( 1 ) {
while( !shache | !key_start );
Dj_star();
for ( i="0" ; bujin_zx_stop & !pri_dj;i++ ){
P1 = 0xf9;
delay ( Delay_time ); // bujin_zx_stop = P3^3;
P1 = 0xfc; // bujin_fx_stop = P3^4;
delay ( Delay_time); // key_puse = P3^0;
P1 = 0xf6; // key_clear = P3^1;
delay ( Delay_time ); // shache = P3^5;
P1 = 0xf3; // pri_dj = P3^6;
delay ( Delay_time );
if( i == set_pwm_width ) { P1 = 0xff; i = 0; one_round_flg = 0; while ( !one_round_flg & key_puse );}
if(!key_puse) { delay(4ms); if(!key_puse) break; }
}
P1 = 0xff;
if ( pri_dj ) break;
if ( !key_puse ) {
delay ( 8ms );
if ( !key_puse ) {
Dj_stop();
while ( !key_puse );
// next pree key
while( !shache );
while(1){
while ( key_puse & key_clear );
delay ( 8ms );
if ( !key_clear ) { round_num = 0; display(); }
if ( !key_puse ) break;
}
while( !key_puse );
delay(8ms);
while( !key_puse );
}
}
}
}
void ext_int0(void) interrupt 0 { /* 主电机 齿轮 中断 */
uint tmp;
EA = 0;
if( !pri_dj ){
round_num ++;
if (round_num % Chilun_Num == 0 ){
one_round_flg = 1;
tmp = round_num / Chilun_Num ;
set_display_num();
P0 = 0xf0;
P0 = P0 | LEDBuf[0] ;
led_1000 = 0;
P0 |= 0xf0;
P0 = 0xf0;
P0 = P0 | LEDBuf[1] ;
led_100 = 0;
P0 |= 0xf0;
P0 = 0xf0;
P0 = P0 | LEDBuf[2] ;
led_10 = 0;
P0 |= 0xf0;
P0 = 0xf0;
P0 = P0 | LEDBuf[3] ;
led_1 = 0;
P0 |= 0xf0;
P0 = 0xf0;
}
if ( round_num >= set_round_num ) Dj_stop();
}
EA = 0x81;
}
void display(){
uchar i;
uint tmp = 0;
tmp = round_num / Chilun_Num ;
set_display_num();
for(i = 0; i < LEDLen ; i ++){
P0 = 0xf0;
P0 = P0 | LEDBuf[i] ;
if(i==0) led_1000 = 0; //P0^4
if(i==1) led_100 = 0; //P0^5
if(i==2) led_10 = 0; //P0^6
if(i==3) led_1 = 0; //P0^7
P0 |= 0xf0;
}
P0 = 0xf0;
}
void read_num(){
/* 读播码盘 到 set_round_num ,set_pwm_width */
uchar tmp;
P2 = 0xFF;
P2 = 0xEF; // 1110 1111
delay ( 1ms );
tmp = ~(P2 | 0xF0);
P2 = 0xDF; // 1101 1111
delay ( 1ms );
tmp = (~(P2 | 0xF0 )) * 10 + tmp;
set_round_num = tmp;
P2 = 0xBF; // 1011 1111
delay ( 1ms );
tmp = (~(P2 | 0xF0));
P2 = 0x7F; // 0111 1111
delay ( 1ms );
tmp = (~(P2 | 0xF0)) * 10 + tmp;
set_round_num = set_round_num + tmp * 100;
set_round_num = set_round_num * Chilun_Num;
P2 = 0xFF;
P1 = 0xbF; // 0111 1111
delay ( 1ms );
tmp = ~(P2 | 0xF0) ;
P1 = 0xFF;
P2 = 0xFF;
P1 &= 0x7F; // 1011 1111
delay ( 1ms );
tmp = (~(P2 | 0xF0)) * 10 + tmp ;
set_pwm_width = tmp ;
P1 = 0xFF;
P2 = 0xFF;
}
void fx_run(){
#define f_Delay_time 180
while ( bujin_fx_stop ) { /* 反向 回车 直到 传感器 动作*/
P1 = 0xf3; //0011
delay ( f_Delay_time );
P1 = 0xf6; //0110
delay ( f_Delay_time );
P1 = 0xfc; //1100
delay ( f_Delay_time );
P1 = 0xf9; //1001
delay ( f_Delay_time );
}
P1 = 0xff;
}
‘肆’ (C语言问题)LCD驱动舍弃RGB位数的编程问题
主要是一个位的移位转换问题。
举个例子:一个24位颜色RGB(0x50,0x60,0x70)转换为565的16位:
r = R & 0xF8 = (01010000 & 11111000) = 01010000(前5位有效)
g = G & 0xFC = (01100000 & 11111100) = 01100000(前6位有效)
b = B & 0xF8 = (01110000 & 11111000) = 01110000(前5位有效)
color是16位的short int类型,高8位由r的5位和g的前3位组合而成。
high就可以写成这样:high = r | (g<<5)?应该是high = r | (g>>5)吧
high = r | (g>>5) = 01010000 | (01100000>>5) = 01010000 | 00000011 = 01010011
low = (g<<3) | (b>>3) = (01100000<<3) | (01110000>>3) = 0000000 | 0001110 = 00001110
所有把低位和高位合起来就是color=(high << 8) | low=(00000000 01010011<<8) | (00000000 00001110) = (01010011 000000000)|(00000000 00001110) = 01010011 00001110 = 01010 011000 01110(565) (这个结果就是取r的前5位,g的前6位,b的前5位)
在microwindow中888向565转换的宏定义如下:更好理解一些
/* create 16 bit 5/6/5 format pixel from RGB colorval (0x00BBGGRR)*/*///c是一个unsigned long型值,0x00BBGGRR
/* In this format, alpha is ignored. #define COLOR2PIXEL565(c) \
((((c) & 0xf8) << 8) | (((c) & 0xfc00) >> 5) | (((c) & 0xf80000) >> 19))
‘伍’ STM32 C编程问题
void LCD_DrawPicture(uint16_t StartX,uint16_t StartY,uint16_t EndX,uint16_t EndY,uint16_t *pic)
{
uint16_t i;
LCD_SetCursor(StartX,StartY);
LCD_WriteRAM_Prepare();
for (i=0;i<(EndX*EndY);i++)
{
LCD_WriteRAM(*pic++);
}
}
个人觉得还得加一个set_window的句子。
这个LCD只定义了起始的地址,以及总的像素大小,没有设定显示屏幕范围,还有屏幕白花花原因1,数据没有读取到。原因2,LCD驱动程序有问题。
‘陆’ 51单片机驱动带有驱动器的步进电机的C语言编程
/*首先你sbit好引脚 ENA使能 DIR方向 PUL脉冲,还有怎么算一周你得试出来的,简单点脉冲拉高,你也不考虑方向DIR你也随便接个IO吧*/
void motor(unsigned char around, unsigned char PWM)
{
for(int i = 0 ;i< around ;i++)
{
ENA =!ENA;
delay_ms(PWM);
}
}