51單片機編程
Ⅰ 用51單片機編程
單片機中只有一條語句是對進制調整的
DA A 對累加器A的結果進行十進制調整
如果隨意對一個累加器A中的二進制數據進行「二進制轉十進制」調整是沒有任何實際意義的!
DA調整的對象是ADD或ADDC以後的結果,而且是BCD碼相加以後才能夠加以調整,否則沒有實際意義!
比如,現在我想執行12D+39D也就是兩個十進制數相加這樣一個加法(有時候程序處理的需要,數據在單片機中是以BCD碼的形式存儲的,也就是12H和39H(十六進制),但我們可以人為將它們看為12D和39D(十進制),而且還希望相加以後的結果為51H,也就是說符合十進制運算規則12+39=51,而不是4BH的結果),但這樣的十進制加法運算在單片機中是不能夠直接實現的。因為單片機只能夠執行二進制加法指令,也就是所有的運算都按照二進制中的規則進行!
於是就出現了DA調整指令!
現在12H+39H,將12H放於A中,執行 ADD A,#39H指令,則結果為4BH,這不是我們希望的51H的數據形式!!這時執行DA A 指令後,就會將A中的數據調整為51H(具體調整過程和原理你可以詳細看書,如果單片機書中講的不詳細,那麼微機原理中一定說的非常詳細),而我們按照BCD碼規則就將其看為51D,符合我們的要求!
也就是說加數和被加數都是BCD碼的形式,最大也只可能是99H,也就是我們十進制數中的99,只有這樣才會有實際的意義!
所以DA A指令只用於十進制BCD碼加法指令以後,否則是沒有實際意義的!
在單片機中數據就是以二進制存放的也可以說是以16進制存放(4位二進制就是16進制),所以一般來說在單片機只有二進制轉十進制才有意義,通過單片機將十進制轉二進制是沒意義的。 這個一般都是我們自己把一個十進制數轉成二進制再送給單片機處理。
非得編寫程序的話 程序如下
----------------------------------------------------------------------
;二進制整數(2位)轉換為十進制整數(組合BCD碼)
;入口: R3,R4
;出口: R0
IBTD22 : MOV A,R0
PUSH A
MOV R7,#03H
CLR A
IBD221 : MOV @R0,A
INC R0
DJNZ R7,IBD221
POP A
MOV R0,A
MOV R7,#10H
IBD222 : PUSH A
CLR C
MOV A,R4
RLC A
MOV R4,A
MOV A,R3
RLC A
MOV R3,A
MOV B,#03H
IBD223 : MOV A,@R0
ADDC A,@R0
DA A
MOV @R0,A
INC R0
DJNZ B,IBD223
POP A
MOV R0,A
DJNZ
--------------------------------------------------
;二進制整數(M位)轉換為十進制整數(組合BCD碼)
;入口: R1,M,N
;出口: R0
IBTDMN : MOV A,R0
MOV R2,A
MOV A,R1
MOV R3,A
MOV B,N
CLR A
IBDMN1 : MOV @R0,A
INC R0
DJNZ B,IBDMN1
MOV A,M
SWAP A
RR A
CLR C
MOV R7,A
IBDMN2 : MOV A,R2
MOV R0,A
MOV A,R3
MOV R1,A
MOV B,M
IBDMN3 : MOV A,@R1
RLC A
MOV @R1,A
INC R1
DJNZ B,IBDMN3
MOV B,N
IBDMN4 : MOV A,@R0
ADDC A,@R0
DA A
JNB ACC.4,IBDMN5
SETB C
CLR ACC.4
IBDMN5 : MOV @R0,A
INC R0
DJNZ B,IBDMN4
DJNZ R7,IBDMN2
MOV A,R2
MOV R0,A
RET
----------------------------------------------------------------
BCD整數轉換為二進制整數(1位)
;入口: R0(高位地址),R7
;出口: R4
IDTB1: CLR A
MOV R4,A
IDB11: MOV A,R4
MOV B,#0AH
MUL AB
ADD A,@R0
INC R0
MOV R4,A
DJNZ R7,IDB11
RET
------------------------------------------
;BCD整數轉換為二進制整數(N位)
;入口: R1(高位地址),M,N
;出口: R0
IDTBMN : MOV A,R0
MOV R2,A
MOV B,N
CLR A
IDBMN1 : MOV @R0,A
INC R0
DJNZ B,IDBMN1
MOV A,R2
MOV R0,A
MOV A,M
MOV NCNT,A
IDBMN2 : MOV R7,N
CLR A
CLR F0
IDBMN3 : XCH A,@R0
MOV B,#0AH
MUL AB
MOV C,F0
ADDC A,@R0
MOV F0,C
MOV @R0,A
INC R0
MOV A,B
DJNZ R7,IDBMN3
MOV A,R2
MOV R0,A
MOV A,@R1
INC R1
ADD A,@R0
MOV @R0,A
DJNZ NCNT,IDBMN2
RET
----------------------------------------------------------
NDIV31 : MOV A,R2
MOV B,R7
DIV AB
PUSH A
MOV R2,B
MOV B,#10H
NDV311 : CLR C
MOV A,R4
RLC A
MOV R4,A
MOV A,R3
RLC A
MOV R3,A
MOV A,R2
RLC A
MOV R2,A
MOV F0,C
CLR C
SUBB A,R7
JB F0,NDV312
JC NDV313
NDV312 : MOV R2,A
INC R4
NDV313 : DJNZ B,NDV311
POP A
CLR OV
JZ NDV314
SETB OV
NDV314 : XCH A,R2
MOV R7,A
RET
Ⅱ 51單片機用什麼軟體編程
開源軟體mcu8051ide 功能不是一般的強,用c語言編程,界面不輸給商業軟體,而且提供多種模擬器件如:led 數碼管,矩陣數碼管(8×8)按鍵,矩陣按鍵,甚至溫度感測器。開源的沒有任何限制,完全免費,自動生成多種燒錄文件(我們一般就用.HEX的)感覺keil c弱爆了。
安裝方法,下載sdcc(mcu8051ide是基於sdcc的)和mcu 8051 ide都安裝好就可以啦,頭文件在sdcc的安裝文件夾內(include)有些關鍵字要加兩個連在一起的下劃線比如 __interrup, __code.
頭文件與keil的不通用,比如定義P0口
在keil下是: sfr P0=0x80; 在sdcc下是: __sfr __at P0 0x80; 這個P0^1的不行,但sdcc裡面有很多頭文件8051.h 80c51.h 80c52.h等等 基本滿足要求,給stc單片機編程不用改,加一點特殊的就可以。這樣優秀的開源軟體不用真的是白不用啊。
Ⅲ 51單片機如何在線編程
用串口線連接單片機,然後用keil軟體設置下就可以在線編程,但是在你改程序的時候,單片機是不運行的
Ⅳ 51單片機c語言編程
對埠輸出操作,直接適用賦值語句即可。 比如 P1 = 0x03;
對埠讀取操作,直接賦值給局部變數即可。
對數碼管操作,需要先定義 對應的 碼表, Tab[16] = {, }分別對應 0~F 的埠輸出值、點亮相應的LED位; 然後直接 操作埠輸出對應字元即可,比如P1=Tab[3] 數碼管輸出顯示3
Ⅳ 關於51單片機程序
MOV DPTR, #ASC_TAB
MOV A, HEX
MOVC A, @A+DPTR
MOV HEX, A
SJMP $
ASC_TAB:
DB 30H, 31H, 32H, 33H, 34H, 35H, 36H, 37H, 38H, 39H
DB 41H, 42H, 43H, 44H, 45H, 46H
Ⅵ 51單片機編程
周期2MS的方波,占空比1:2,不就是1MS高電平1MS低電平?
Ⅶ 51單片機C語言編程
你的設計有點問題,不過你看下這個程序對你應該有幫助。
#include <reg52.h>
#define int8 unsigned char
#define int16 unsigned int
#define int32 unsigned long
int8 flag;
int8 n;
int8 code table[][32]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/* " " */
{0x04,0x28,0x08,0x24,0x32,0x22,0xC2,0x21,0xC2,0x26,0x34,0x38,0x04,0x04,0x08,0x18,0x30,0xF0,0xC0,0x17,0x60,0x10,0x18,0x10,0x0C,0x14,0x06,0x18,0x04,0x10,0x00,0x00},/*"歡",0*/
{0x02,0x02,0x04,0x82,0xF8,0x73,0x04,0x20,0x02,0x00,0xE2,0x3F,0x42,0x20,0x82,0x40,0x02,0x40,0xFA,0x3F,0x02,0x20,0x42,0x20,0x22,0x20,0xC2,0x3F,0x02,0x00,0x00,0x00},/*"迎",1*/
{0x00,0x01,0x04,0x02,0x1C,0x0C,0xC0,0x3F,0x1C,0xC0,0x02,0x09,0x02,0x16,0x92,0x60,0x4A,0x20,0x82,0x2F,0x02,0x20,0x0E,0x24,0x00,0x22,0x90,0x31,0x0C,0x20,0x00,0x00},/*"您",2*/
{0x44,0x10,0x44,0x14,0x88,0x54,0x88,0x54,0xD0,0x5F,0x30,0x50,0x60,0x50,0xFE,0x7F,0x40,0x50,0x20,0x90,0xB0,0x9F,0x98,0x92,0x8C,0x94,0x88,0x34,0x80,0x11,0x00,0x00},/*"乘",3*/
{0x82,0x00,0x02,0x01,0x22,0x06,0x22,0x38,0x22,0x04,0x22,0x03,0x22,0x00,0xFE,0xFF,0xA2,0x00,0x22,0x01,0x22,0x02,0x22,0x3C,0x22,0x02,0x02,0x01,0x02,0x00,0x00,0x00},/*"坐",4*/
{0x02,0x00,0x0C,0x00,0xF0,0x3F,0x00,0x20,0x00,0x20,0x00,0x20,0x00,0x20,0x00,0xA0,0x00,0x60,0x00,0x20,0x00,0x20,0x00,0x20,0x00,0x20,0x00,0x20,0x00,0x20,0x00,0x00},/*"廣",5*/
{0x80,0x00,0x01,0x07,0x06,0x00,0x18,0x00,0xE0,0xFF,0x00,0x04,0x00,0x03,0x00,0x00,0xFE,0x7F,0x00,0x08,0x00,0x06,0x80,0x01,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00},/*"州",6*/
{0x00,0x00,0x04,0x20,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x21,0x04,0x20,0x04,0x00,0x00,0x00},/*"三",7*/
{0x20,0x08,0x20,0x86,0x7F,0x60,0x80,0x0F,0x00,0x04,0x00,0x1A,0x00,0xEA,0x00,0x2A,0x00,0x2A,0x00,0x2A,0x00,0x2A,0xF0,0x2B,0x0C,0x28,0x02,0x20,0x0F,0x00,0x00,0x00},/*"汽",8*/
{0x00,0x00,0x80,0x00,0x04,0x01,0x0E,0x02,0x14,0x0C,0x24,0x70,0xC4,0x21,0x8C,0x00,0x08,0x00,0x08,0x70,0x28,0x08,0x1E,0x06,0x0C,0x03,0x80,0x01,0x00,0x01,0x00,0x00},/*"公",9*/
{0x00,0x00,0x00,0x08,0xF8,0x49,0x20,0x49,0x20,0x49,0x20,0x49,0x20,0x49,0x20,0x49,0x20,0x49,0xF0,0x49,0x00,0x48,0x04,0x40,0x02,0x40,0xFC,0x7F,0x00,0x00,0x00,0x00},/*"司",10*/
{0x00,0x00,0x80,0x00,0x04,0x01,0x0E,0x02,0x14,0x0C,0x24,0x70,0xC4,0x21,0x8C,0x00,0x08,0x00,0x08,0x70,0x28,0x08,0x1E,0x06,0x0C,0x03,0x80,0x01,0x00,0x01,0x00,0x00},/*"公",11*/
{0x40,0x00,0x40,0x08,0x41,0x08,0x42,0x08,0x4C,0x08,0xF8,0x7F,0x50,0x08,0x40,0x08,0x40,0x08,0x60,0x08,0xD0,0x7F,0x4C,0x08,0x47,0x08,0x42,0x08,0x40,0x00,0x00,0x00},/*"共",12*/
{0x20,0x08,0x20,0x86,0x7F,0x60,0x80,0x0F,0x00,0x04,0x00,0x1A,0x00,0xEA,0x00,0x2A,0x00,0x2A,0x00,0x2A,0x00,0x2A,0xF0,0x2B,0x0C,0x28,0x02,0x20,0x0F,0x00,0x00,0x00},/*"汽",13*/
{0x20,0x00,0x20,0x20,0x20,0x21,0x20,0x23,0x20,0x25,0x20,0x39,0x20,0xE1,0x20,0x21,0xFF,0x2F,0x20,0x21,0x20,0x21,0x20,0x21,0x20,0x21,0x20,0x21,0x20,0x00,0x00,0x00},/*"車",14*/
{0x00,0x00,0x00,0x00,0x1A,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*",",15*/
{0x00,0x04,0x01,0x04,0x02,0x04,0x04,0x04,0x08,0x04,0x30,0x04,0xC0,0x05,0x00,0xFE,0x80,0x05,0x60,0x04,0x10,0x04,0x0C,0x04,0x06,0x04,0x03,0x04,0x02,0x04,0x00,0x00},/*"大",16*/
{0x00,0x02,0x40,0x0C,0x40,0x08,0x40,0x48,0x40,0x3A,0x40,0x2A,0x42,0x0A,0x41,0x8A,0xFE,0x7A,0x40,0x2B,0x40,0x0A,0x40,0x18,0x40,0xEA,0x40,0x4C,0x40,0x08,0x00,0x00},/*"學",17*/
{0x20,0x08,0x30,0x08,0xE0,0xFF,0x42,0x08,0x4C,0x08,0xF0,0x1F,0x10,0x12,0x1A,0x12,0xF2,0x13,0x04,0x10,0xC8,0xFF,0x30,0x10,0xCC,0x50,0x02,0x37,0x1C,0x12,0x00,0x00},/*"城",18*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x0E,0x14,0x10,0x24,0x10,0x44,0x10,0x84,0x11,0x0C,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"2",0*/
{0x04,0x02,0x44,0x06,0xC4,0x1A,0x48,0xE3,0x48,0x46,0x48,0x00,0x04,0x09,0x04,0x09,0x08,0x09,0xC8,0xFF,0x30,0x09,0x28,0x49,0x44,0x39,0x02,0x29,0x1F,0x01,0x00,0x00},/*"線",19*/
{0x00,0x02,0x02,0x42,0x04,0x42,0x08,0x42,0x30,0x42,0xC0,0x7F,0x00,0x42,0x00,0x42,0x00,0x42,0x00,0x42,0xFE,0x7F,0x00,0x42,0x00,0x42,0x00,0x42,0x00,0x42,0x00,0x00},/*"開",20*/
{0x40,0x04,0x80,0x08,0x00,0x11,0xFF,0xEF,0x00,0x44,0x02,0x10,0x82,0x10,0x82,0x10,0x82,0x90,0xFE,0x7F,0x82,0x50,0x82,0x10,0x82,0x10,0x82,0x10,0x02,0x10,0x00,0x00},/*"往",21*/
{0x00,0x04,0x01,0x04,0x02,0x04,0x04,0x04,0x08,0x04,0x30,0x04,0xC0,0x05,0x00,0xFE,0x80,0x05,0x60,0x04,0x10,0x04,0x0C,0x04,0x06,0x04,0x03,0x04,0x02,0x04,0x00,0x00},/*"大",22*/
{0x00,0x02,0x40,0x0C,0x40,0x08,0x40,0x48,0x40,0x3A,0x40,0x2A,0x42,0x0A,0x41,0x8A,0xFE,0x7A,0x40,0x2B,0x40,0x0A,0x40,0x18,0x40,0xEA,0x40,0x4C,0x40,0x08,0x00,0x00},/*"學",23*/
{0x20,0x08,0x30,0x08,0xE0,0xFF,0x42,0x08,0x4C,0x08,0xF0,0x1F,0x10,0x12,0x1A,0x12,0xF2,0x13,0x04,0x10,0xC8,0xFF,0x30,0x10,0xCC,0x50,0x02,0x37,0x1C,0x12,0x00,0x00},/*"城",24*/
{0x00,0x00,0x00,0x00,0x1A,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*",",25*/
{0x00,0x02,0x00,0x42,0x00,0x32,0xFE,0x23,0x04,0x00,0x08,0x44,0xFF,0x55,0x50,0x55,0x50,0x55,0x50,0xFD,0x52,0x55,0x51,0x55,0xFE,0x55,0x00,0x44,0x00,0x04,0x00,0x00},/*"請",26*/
{0x00,0x01,0x04,0x02,0x1C,0x0C,0xC0,0x3F,0x1C,0xC0,0x02,0x09,0x02,0x16,0x92,0x60,0x4A,0x20,0x82,0x2F,0x02,0x20,0x0E,0x24,0x00,0x22,0x90,0x31,0x0C,0x20,0x00,0x00},/*"您",27*/
{0x82,0x00,0x02,0x01,0x22,0x06,0x22,0x38,0x22,0x04,0x22,0x03,0x22,0x00,0xFE,0xFF,0xA2,0x00,0x22,0x01,0x22,0x02,0x22,0x3C,0x22,0x02,0x02,0x01,0x02,0x00,0x00,0x00},/*"坐",28*/
{0x01,0x08,0xC2,0x08,0x44,0x0F,0x28,0xF8,0x30,0x08,0xCE,0x0F,0x04,0x01,0x00,0x41,0x02,0x41,0x01,0x41,0xFE,0x4F,0x00,0x51,0x00,0x61,0x00,0x41,0x00,0x01,0x00,0x00},/*"好",29*/
{0x42,0x08,0x81,0x08,0xFE,0xFF,0x00,0x09,0x01,0x0A,0x01,0x01,0x02,0x11,0x0C,0x11,0x30,0x11,0xC0,0xFF,0x30,0x11,0x08,0x11,0x06,0x11,0x03,0x11,0x02,0x01,0x00,0x00},/*"扶",30*/
{0x20,0x08,0xC0,0x48,0x00,0x4B,0xFF,0x7F,0x00,0x89,0x9C,0x88,0x00,0x10,0xBC,0x2A,0x82,0xCA,0xC2,0x4A,0xB2,0x5A,0x82,0x6A,0xAE,0x0A,0x90,0x0F,0x0C,0x00,0x00,0x00},/*"穩",31*/
{0x00,0x00,0x00,0x00,0x00,0x00,0xFA,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",32*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}/* " " */
};
void delay(void);
int16 offset;
void main(void)
{
int8 i;
int8 *p;
flag=0x10;
n=0;
TMOD=0x01;
TH0=0xb1;
TL0=0xe0;
ET0=1;
EA=1;
TR0=1;
p=&table[0][0];
while (1)
{
for (i=0;i<8;i++) //顯示左半邊屏幕
{
P0=*(p+offset+2*i);
P2=i|0x08; //P2.4=0,P2.3=1 選中U2, 輸出掃描碼給U6
delay();
P0=*(p+offset+2*i+1);
P2=i|0x10; //P2.4=1,P2.3=0 選中U3, 輸出掃描碼給U7
delay();
}
for (i=8;i<16;i++) //顯示右半邊屏幕
{
P0=*(p+offset+2*i);
P2=(i-8)|0x20; //P2.5=1 P2.4=0, P2.3=0 選中U4,輸出掃描碼U8
delay();
P0=*(p+offset+2*i+1);
P2=(i-8)|0x40; //P2.6=1 P2.5=0, P2.4=0 選中U5,輸出掃描碼U9
delay();
}
}
}
void delay(void)
{
int16 i;
for (i=0;i<50;i++)
;
}
void timer0() interrupt 1 using 3
{
TF0=0;
TH0=0xb1;
TL0=0xe0;
if (n<10)
{
n++;
}
else
{
offset+=2;
if (offset>1100)
offset=0;
n=0;
}
}
Ⅷ 關於51單片機的程序
MOV B, #100
DIV AB
MOV 21H, A
MOV A, #10
XCH A, B
DIV AB
SWAP A
ADD A, B
MOV 22H, A
SJMP $
Ⅸ 51單片機編程
若要將P1.0口設置為高電平,如何編寫程序
前面定義 sbit P10 = P1 ^ 0;
後面 P10 = 1;
若要檢測P1口上8個引腳是否全為高電平用什麼語句
先配置P1口8個引腳為輸入狀態: P1 = 0xFF;
然後判定條件示例 if (P1 == 0xFF)
Ⅹ MC51單片機編程
現在我還沒法編,沒有相應的軟體,等明天後天我給你編一個