炸香机 发表于 2014-12-5 13:37 很久之前写的,希望你用得上 |
#include<reg52.h> #include<stdlib.h> #include<stdio.h> #define unint unsigned int #define unchar unsigned char sbit en_seg=P2^6; sbit en_cell=P2^7; sbit buzzer=P2^3; sbit signal_out=P3^5; sbit knob_a=P1^0; sbit knob_b=P1^1; char knob_state; unchar code seg_table[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x80,0x48,0x40,0x5e,0x79,0x71,0x00,0x40}; unchar i,j1,math,equal,buz_t; unchar time_h,time_l,knob_value=150; unchar digit[]={0,16,16,16,16,16}; void display(); void buzzer_(); void knob(); void out_convert(unchar); void T0T1_init() { TMOD=0x11; TH1=0xe3; TL1=0x33; EA=1; ET0=1; ET1=1; TR1=1; } void T1_2ms() interrupt 3 { TH1=0xf1; TL1=0x99; i++; display(); buzzer_(); knob(); if(i>2) { i=0; TH0=time_h; TL0=time_l; signal_out=1; TR0=1; } } void T0_signal() interrupt 1 { signal_out=0; TR0=0; } void display() { switch(i%3) { case 0: P0=0xFF; en_cell=1; P0=0xf7; en_cell=0; P0=0x00; en_seg=1; P0=seg_table[digit[2]]; en_seg=0; break; case 1: P0=0xFF; en_cell=1; P0=0xef; en_cell=0; P0=0x00; en_seg=1; P0=seg_table[digit[1]]; en_seg=0; break; case 2: P0=0xFF; en_cell=1; P0=0xdf; en_cell=0; P0=0x00; en_seg=1; P0=seg_table[digit[0]]; en_seg=0; break; } } void buzzer_() { if(buzzer==0) { buz_t--; if(buz_t==0) { buzzer=1; } } } void out_convert(unchar dataout) { unchar out_convert_j; digit[0]=dataout%10; for(out_convert_j=1;out_convert_j<6;out_convert_j++) { if(dataout>=10) { dataout=dataout/10; digit[out_convert_j]=dataout%10; } else { digit[out_convert_j]=16; } } } void knob() { switch(P1&0x03) { case 0: if(knob_state==1) { knob_state=2; } if(knob_state==-1) { knob_state=-2; } break; case 1: if(knob_state==0) { knob_state=1; } if(knob_state==-2) { knob_state=-3; } break; case 2: if(knob_state==0) { knob_state=-1; } if(knob_state==2) { knob_state=3; } break; case 3: if(knob_state==3) { buzzer=0; buz_t=5; if(knob_value>50) { knob_value--; out_convert(knob_value); } } if(knob_state==-3) { buzzer=0; buz_t=5; if(knob_value<250) { knob_value++; out_convert(knob_value); } } knob_state=0; break; } } void main() { unint temp; T0T1_init(); out_convert(knob_value); buzzer=0; buz_t=50; signal_out=0; while(1){ temp=knob_value/0.05425; time_h=(65536-temp)/256; time_l=(65536-temp)%256; } } 很久以前写的,现在自己也看不太懂了,希望你用得上 |
天边一只雁 发表于 2014-12-5 09:16 我说的是单片机产生PWM信号的代码 |
炸香机 发表于 2014-12-4 16:43 单片机只是产生控制信号,和LED驱动无关,舵机板改造只涉及几个器件,无须任何代码 |
天边一只雁 发表于 2014-12-4 14:18 不错 我也在学习这款单片机 楼主可以把源程序代码发给我吗 邮箱是 704478068@qq.com 谢谢 |
炸香机 发表于 2014-12-4 13:09 STC89C52,最普通的51系列 |
不错 顶楼主 请问楼主开发板的单片机是什么型号 |