C51原理及相關(guān)基礎(chǔ)入門知識(shí)
第一章:C51 流程控制語句一、分類條件語句、循環(huán)語句和開關(guān)語句。下面將對(duì)這些語句作詳細(xì)介紹。(1) 條件語句條件語句的一般形式為:if(表達(dá)式)語句 1;else語句 2;上述結(jié)構(gòu)表示: 如果表達(dá)式的值為非0(TURE)即真, 則執(zhí)行語句1, 執(zhí)行完語句1 從語句2 后開始繼續(xù)向下執(zhí)行; 如果表達(dá)式的值為 0(FALSE)即假, 則跳過語句1 而執(zhí)行語句2。所謂表達(dá)式是指關(guān)系表達(dá)式和邏輯表達(dá)式的結(jié)合式。注意:1. 條件執(zhí)行語句中"else 語句2;"部分是選擇項(xiàng), 可以缺省, 此時(shí)條件語句變成:if(表達(dá)式) 語句1;表示若表達(dá)式的值為非 0 則執(zhí)行語句1 , 否則跳過語句1 繼續(xù)執(zhí)行。2. 如果語句1 或語句2 有多于一條語句要執(zhí)行時(shí), 必須使用"{"和"}" 把這些語句包括在其中, 此時(shí)條件語句形式為:if(表達(dá)式){語句體 1;}else{語句體 2;}3. 條件語句可以嵌套, 這種情況經(jīng)常碰到, 但條件嵌套語句容易出錯(cuò), 其原因主要是不知道哪個(gè)if 對(duì)應(yīng)哪個(gè)else。例如:if(x>20||x<-10)if(y<=100&&y>x)printf("Good");elseprintf("Bad");對(duì)于上述情況,規(guī)定: else 語句與最近的一個(gè)if 語句匹配, 上例中的 else 與 if(y<=100&&y>x) 相匹配。為了使 else 與if(x>20||x<-10) 相匹配, 必須用花括號(hào)。如下所示:if(x>20||x<-10){if(y<=100&&y>x)printf("Good");}
標(biāo)簽:
C51
入門知識(shí)
上傳時(shí)間:
2013-10-24
上傳用戶:Sophie
16 16點(diǎn)陣顯示漢字原理及顯示程序
#include "config.h"
#define DOTLED_LINE_PORT PORTB #define DOTLED_LINE_DDR DDRB #define DOTLED_LINE_PIN PINB
#define DOTLED_LINE_SCKT PB1 #define DOTLED_LINE_SCKH PB5 #define DOTLED_LINE_SDA PB3
#define DOTLED_ROW_PORT PORTC #define DOTLED_ROW_DDR DDRC #define DOTLED_ROW_PIN PINC
#define DOTLED_ROW_A0 PC0 #define DOTLED_ROW_A1 PC1 #define DOTLED_ROW_A2 PC2 #define DOTLED_ROW_A3 PC3 #define DOTLED_ROW_E PC4 uint8 font[] = { /*-- 調(diào)入了一幅圖像:這是您新建的圖像 --*/ /*-- 寬度x高度=16x16 --*/ 0x00,0x00,0x00,0x00,0x08,0x38,0x18,0x44,0x08,0x44,0x08,0x04,0x08,0x08,0x08,0x10, 0x08,0x20,0x08,0x40,0x08,0x40,0x08,0x40,0x3E,0x7C,0x00,0x00,0x00,0x00,0x00,0x00 }; static void TransmitByte(uint8 byte); static void SelectRow(uint8 row); static void FlipLatchLine(void);
static void TransmitByte(uint8 byte) { uint8 i; for(i = 0 ; i < 8 ; i ++) { if(byte & (1 << i)) { DOTLED_LINE_PORT |= _BV(DOTLED_LINE_SDA); } else { DOTLED_LINE_PORT &= ~_BV(DOTLED_LINE_SDA); } //__delay_cycles(100); DOTLED_LINE_PORT |= _BV(DOTLED_LINE_SCKH); //__delay_cycles(100); DOTLED_LINE_PORT &= ~_BV(DOTLED_LINE_SCKH); //__delay_cycles(100); } }
static void SelectRow(uint8 row) { //row -= 1; row |= DOTLED_ROW_PIN & 0xe0; DOTLED_ROW_PORT = row; }
static void FlipLatchLine(void) { DOTLED_LINE_PORT |= _BV(DOTLED_LINE_SCKT); DOTLED_LINE_PORT &= ~_BV(DOTLED_LINE_SCKT); }
void InitDotLedPort(void) { DOTLED_LINE_PORT &= ~(_BV(DOTLED_LINE_SCKT) | _BV(DOTLED_LINE_SCKH)); DOTLED_LINE_PORT |= _BV(DOTLED_LINE_SDA); DOTLED_LINE_DDR |= _BV(DOTLED_LINE_SCKT) | _BV(DOTLED_LINE_SCKH) | _BV(DOTLED_LINE_SDA); DOTLED_ROW_PORT |= 0x1f; DOTLED_ROW_PORT &= 0xf0; DOTLED_ROW_DDR |= 0x1f; } void EnableRow(boolean IsEnable) { if(IsEnable) { DOTLED_ROW_PORT &= ~_BV(DOTLED_ROW_E); } else { DOTLED_ROW_PORT |= _BV(DOTLED_ROW_E); } } void PrintDotLed(uint8 * buffer) { uint8 i , tmp; for(i = 0 ; i < 16 ; i ++) { tmp = *buffer ++; TransmitByte(~tmp); tmp = *buffer ++; TransmitByte(~tmp); SelectRow(i); FlipLatchLine(); } } void main(void) { InitDotLedPort(); EnableRow(TRUE); while(1) { PrintDotLed(font); __delay_cycles(5000); } }
//---------------------------------------------------- config.h文件
#ifndef _CONFIG_H #define _CONFIG_H
//#define GCCAVR
#define CPU_CYCLES 7372800L
#ifndef GCCAVR #define _BV(bit) (1 << (bit)) #endif
#define MSB 0x80 #define LSB 0x01
#define FALSE 0 #define TRUE 1 typedef unsigned char uint8; typedef unsigned int uint16; typedef unsigned long uint32; typedef unsigned char boolean; #include <ioavr.h> #include <inavr.h>
#include "dotled.h" #endif
//-----
標(biāo)簽:
16
點(diǎn)陣顯示
漢字
顯示程序
上傳時(shí)間:
2013-11-18
上傳用戶:mnacyf