You are on page 1of 18

Wednesday, July 23, 2008

Lp trine C cho VXL - C bn

bkav huy231086@yahoo.com

Lp trnh C cho VXL 8051 - C bn


I.Gii thiu
i. C l mt ngn ng kh mnh v rt nhiu ngi dng .Nu ni s lnh c bn ca C th khng nhiu . Nhng i vi lp trnh cho vxl , chng ta chi cn bit s lng lnh khng nhiu . u tin bn phi lm quen vi ii. iii. iv. v. Cc kiu ton t C Cc kiu d liu Cu trc c bn ca mt chng trnh Cc cu trc iu khin (chnh cc tp lnh ) 1. Cu trc iu kin : if v else 2. Cc cu trc lp a. Vng lp while b. Vng lp do while c. Vng lp for 3. Lnh break. 4. Cu trc la chn: switch. case

vi. Bit s dng cc hm v chng trnh con.

vii. II.C bn C
1. Cc ch th trc x l ca Keil C
viii. // chu thich

chu thich*** a. * b. */ ii. y l dng ch thch. Tt c cc dng bt u bng hai du s (//) c coi l cht thch m chng khng c bt k mt nh hng no n hot ng ca chng trnh. Chng c th c cc lp trnh vin dng gii thch hay bnh phm bn trong m ngun ca chng trnh. Trong trng hp ny, dng ch thch l mt gii thch ngn gn nhng g m chng trnh chng ta lm. Cn trong du (/* */) bn c th ch thch bao nhiu dng tu thch ,

Lp trine C cho VXL - C bn

bkav huy231086@yahoo.com

#include <AT89X52.H> #include "AT89X52.H" trnh bin dch s gi file th vin ca 89 ra (c bn l 51 cng nh 52)

#define bien_thay_the bien Vd #define Congtac P0_6 port0.6 c t tn l congtac , khi ta gi tn ny trnh bin dch Keil s t chuyn ti bit qun l P0_6 Note :cch vit P0_6 ph thuc vo tng trnh bin dch , c chng trnh th li vit l P0.6 , cn keil C vit nh cch u

#define m_left_tien P1_5 #define m_left_lui P1_4 #define m_left_forward m_left_tien=0;m_left_lui=1; cc bn ch y l mt cch s dng marco trong C khi mnh gi m_left_forward th chn P1_5 = 0 v P1_4=1 Cc vit ny gn nh cho chng ta mt chng trnh con , tuy nhin khng nn qu lm dng n Mt u im ni bt ca C l cc bn c th to ra cc b th vin . V d sau l to th vin thuvien.h (ui .h bn c th to bng cch save as .. *.h Keil C ). #ifndef _thuvien_H #define _thuvien_H .//m chng trnh #endif

2. Cc ton t :
->Ton t gn (=). Ex:

Lp trine C cho VXL - C bn

bkav huy231086@yahoo.com

b a a a

= = = =

5; 2 + b; 2 + (b = 5); b = c = 5;

->Cc ton t s hc ( +, -, *, /, % ) + cng - tr * nhn / chia % ly phn d (trong php chia) ->Cc ton t gn phc hp (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) value += increase; tng ng vi value = value + increase; a = 5; tng ng vi a = a - 5; a /= b; tng ng vi a = a / b; price *= units + 1; tng ng vi price = price * (units + 1); Tng v gim ++ --

a++; <=> a+=1; <=> a=a+1;


tnh cht tin t hoc hu t (++a) # (a++) Ex B=3; B=3;A=++B; // A is 4, B is 4 B=3; A=B++; // A is 3, B is 4 -> Cc ton t quan h ( ==, !=, >, <, >=, <= )

== Bng != Khc > Ln hn < Nh hn > = Ln hn hoc bng < = Nh hn hoc bng
EX

Lp trnh C cho VXL - C bn

bkav huy231086@yahoo.com

(7 == 5) s tr gi tr false (6 >= 6) s tr gi tr true


tt nhin thay v s dng cc s, chng ta c th s dng bt c biu thc no. Cho a=2, b=3 v c=6

(a*b >= c) s tr gi tr true. (b+4 < a*c) s tr gi tr false


Ch rng = (mt du bng) lf hon ton khc vi == (hai du bng). (==) nhm so snh cn (=)gn gi tr ca biu thc bn phi cho bin bn tri . -> Cc ton t logic ( !, &&, || ). ! NOT && AND || OR EX:

!(5 == 5) tr v false v biu thc bn phi (5 == 5) c gi tr true. !(6 <= 4) tr v true v (6 <= 4)c gi tr false. !true tr v false. !false tr v true. ( (5 == 5) && (3 > 6) ) tr v false ( true && false ). ( (5 == 5)
-> Cc ton t thao tc bit ( &, |, ^, ~, <<, >> ). & AND Logical AND | OR Logical OR ^ XOR Logical exclusive OR ~ NOT o ngc bit << SHL Dch bit sang tri >> SHR Dch bit sang phi ->Th t u tin ca cc ton t

Th t 1

Ton t
::

M t scope

Lp trnh C cho VXL - C bn

bkav huy231086@yahoo.com

() [ ] -> . sizeof ++ -~ !

tng/gim o ngc bit NOT Ton t con tr Chuyn i kiu Dng hoc m Ton t s hc Ton t s hc Dch bit Ton t quan h Ton t quan h Ton t thao tc bit Ton t logic Ton t iu kin Ton t gn Du phy

& * (type) + -

4 5 6 7 8 9 10 11 12 13

* / % + << >> < <= > >= == != & ^ | && || ?: = += -= *= /= %= >>= <<= &= ^= |= ,

3. Cc kiu d liu
Cc kiu bin.chun Type char unsigned char enum short unsigned short

Bits
8 8 16 16 16

Bytes Range
1 1 2 2 2 128 to +127 0 to 255 32,768 to +32,767 32,768 to +32,767 0 to 65,535

Lp trnh C cho VXL - C bn

bkav huy231086@yahoo.com

int unsigned int long unsigned long

16 16 32 32

2 2 4 4

32,768 to +32,767 0 to 65,535 2,147,483,648 to +2,147,483,647 0 to 4,294,697,295

Kiu d liu trong Keil C

Type bit sbit sfr sf16

Bits
1 1 8 16

Bytes Range
0 0 1 2 0 to 1 0 to 1 0 to 255 0 to 65,535

4. Cu trc c bn ca 1 chng trnh C


//Cc ch th tin nh #include <lcd.h> //Gi th vin c sn cch vit khc "*.h" #define led1 PORTA.0 //dng nh ngha cc bin char bien1,bien2; //cac bien can dung int a,b; void chuongtrinhcon(unsigned int b) { } int ham(void) { . Return(a); } // chuong trinh con

// chuong trinh con dang ham

Lp trnh C cho VXL - C bn

bkav huy231086@yahoo.com

void main(void) //chng trnh chnh { int a; // khai bao bien dang so nguyen chuongtrinhcon(); a = ham(); } Chng trnh con l ni cc bn vit cc chng trnh nh , rt tin cho cc on lnh gp li nhiu ln . Chng trnh con c th c th gi trong chng trnh chnh bt k u . Hm l chng trnh con tr v cho mnh mt gi tr. Cch s dng hm v chng trnh con cc bn nn tham kho thm quyn k thut lp trnh C hiu r hn .

5. Cc lnh c bn ca C
Cu trc iu kin: if v else ->if (condition) statement if (x == 100) x++; nu x=100 th tng x thm 1 ->if (condition) statement1 else statement2

if (x == 100) else x- -;
Cc cu trc lp

x++;

Vng lp while . Dng ca n nh sau: while (expression) statement while(1) {}; To vng lp mi mi , rt hay ng trong lp trnh VXL .Chng trnh chnh s c vit trong du ngoc. Vng lp do-while Dng thc: do statement while (condition); Ex: do {

Lp trnh C cho VXL - C bn

bkav huy231086@yahoo.com

x++; // cho nay cac ban co the viet nhieu cau lenh , } while(x>10) tng gi tr ca x cho n khi x > 10 Chc nng ca n l hon ton ging vng lp while ch tr c mt iu l iu kin iu khin vng lp c tnh ton sau khi statement c thc hin, v vy statement s c thc hin t nht mt ln ngay c khi condition khng bao gi c tho mn .Nh vd trn k c x >10 th n vn tng gi tr 1 ln trc khi thot

nu x=100 th tng x thm 1 cn khng th gim x. Vng lp for . Dng thc: for (initialization; condition; increase) statement; v chc nng chnh ca n l lp li statement chng no condition cn mang gi tr ng, nh trong vng lp while. Nhng thm vo , for cung cp ch dnh cho lnh khi to v lnh tng. V vy vng lp ny c thit k c bit lp li mt hnh ng vi mt s ln xc nh. Cch thc hot ng ca n nh sau: 1, initialization c thc hin. Ni chung n t mt gi kh ban u cho bin iu khin. Lnh ny c thc hin ch mt ln. 2, condition c kim tra, nu n l ng vng lp tip tc cn nu khng vng lp kt thc v statement c b qua. 3, statement c thc hin. N c th l mt lnh n hoc l mt khi lnh c bao trong mt cp ngoc nhn. 4, Cui cng, increase c thc hin tng bin iu khin v vng lp quay tr li bc 2. Phn khi to v lnh tng khng bt buc phi c. Chng c th c b qua nhng vn phi c du chm phy ngn cch gia cc phn. V vy, chng ta c th vit for (;n<10;) hoc for (;n<10;n++). Bng cch s dng du phy, chng ta c th dng nhiu lnh trong bt k trng no trong vng for, nh l trong phn khi to. V d chng ta c th khi to mt lc nhiu bin trong vng lp: for ( n=0, i=100 ; n!=i ; n++, i-- ) { // ci g y cng c... } Vi d in hnh nht trong lp trnh VXL
void delayms(int n) { int i,j; for (i=0;i<n;i++) for (j=0;j<1500;j++) // khai bao bien chi trong chuong trinh con { } // tham so j tuy thach anh toc do vxl ma cac

Lp trnh C cho VXL - C bn

bkav huy231086@yahoo.com

//ban thay doi cho phu hop } Cc lnh r nhnh v lnh nhy Lnh break. S dng break chng ta c th thot khi vng lp ngay c khi iu kin n kt thc cha c tho mn. Lnh ny c th c dng kt thc mt vng lp khng xc nh hay buc n phi kt thc gia chng thay v kt thc mt cch bnh thng. V d, chng ta s dng vic m ngc trc khi n kt thc: Lnh continue. Lnh continue lm cho chng trnh b qua phn cn li ca vng lp v nhy sang ln lp tip theo. V d chng ta s b qua s 5 trong phn m ngc: Lnh goto. Lnh ny cho php nhy v iu kin ti bt k im no trong chng trnh. Ni chung bn nn trnh dng n trong chng trnh C++. Tuy nhin chng ta vn c mt v d dng lnh goto m ngc: Hm exit. Mc ch ca exit l kt thc chng trnh v tr v mt m xc nh. Dng thc ca n nh sau void exit (int exit code); exit code c dng bi mt s h iu hnh hoc c th c dng bi cc chng trnh gi. Theo quy c, m tr v 0 c ngha l chng trnh kt thc bnh thng cn cc gi tr khc 0 c ngha l c li. cc lnh trn mnh ch yu ch dng lnh break thot khi vng lp . Cc lnh khc thng rt t dc s dng y l 1 on code nh mnh trch ra t chng trnh ca mnh while(1) { lcd_gotoxy(5,0);lcd_putsf(" Run Thuan thuan(); if(!enter) { lcd_clear(); lcd_putsf("DA DUNG "); stop();break;} }

");

Cu trc la chn: switch. C php ca lnh switch hi c bit mt cht. Mc ch ca n l kim tra mt vi gi tr hng cho mt biu thc, tng t vi nhng g chng ta lm u bi ny khi lin kt mt vi lnh if v else if vi nhau. Dng thc ca n nh sau: Code:
switch (expression) {

Lp trnh C cho VXL - C bn

bkav huy231086@yahoo.com

case constant1: block of instructions 1 break; case constant2: block of instructions 2 break;

default: default block of instructions }

N hot ng theo cch sau: switch tnh biu thc v kim tra xem n c bng constant1 hay khng, nu ng th n thc hin block of instructions 1 cho n khi tm thy t kho break, sau nhy n phn cui ca cu trc la chn switch. Cn nu khng, switch s kim tra xem biu thc c bng constant2 hay khng. Nu ng n s thc hin block of instructions 2 cho n khi tm thy t kho break. Cui cng, nu gi tr biu thc khng bng bt k hng no c ch nh trn (bn c th ch nh bao nhiu cu lnh case tu thch), chng trnh s thc hin cc lnh trong phn default: nu n tn ti v phn ny khng bt buc phi c.
nu nt enter c bm th chng trnh s thot ra khi vng lp while .

III. C cho 8051

1. Keil Variable Extensions


data a ch trc tip MOV A, 07Fh idata a ch gin tip MOV R0, #080h MOV A, R0 xdata b nh ngoi MOVX @DPTR code b nh chng trnh MOVC @A+DPTR

VD unsigned int data bien = 0; // them data vao khai bao kieu bien Ch rng , bn c th khng cn khai bo c th , ch cn unsigned int checksum = 0;

2. a ch ngt
Interrupt External 0 Vector address 0003h Interrupt number 0

Lp trnh C cho VXL - C bn

bkav huy231086@yahoo.com

Timer 0 External 1 Timer 1 Serial

000Bh 0013h 001Bh 0023h

1 2 3 4

org ljmp main org 0003h ljmp ngat0 org 30h main:

00h

// chuong trinh chinh // vong lap vo tan

Here: sjmp Here ngat0: reti

code C Code:
void main { // chuong trinh chinh

while(1) //vong lap vo tan sau khi thuc hien xong cong viec } void ngat0(void) interrupt 0 // chuong trinh ngat { }

3 .Mt v d hon thin v lp trnh C cho 8051


Chng trnh delay c bn: #define high(x) (((65536-x) & 0xFF00) >> 8) #define low(x) ((unsigned char)(65536-x)) void delay (unsigned int time) { while (time--) { TMOD = 0X01; TH0 = high(1000); TL0 = low(1000); TF0 = 0; TR0 = 1; while(!TF0); TR0 = 0; } }

Lp trnh C cho VXL - C bn

bkav huy231086@yahoo.com

m t v d v l p tr nh C y .
#include <intrins.h> #include <reg51f.h> #include <math.h> #include <stdio.h> #include <define_a.c> /***************** to define sensor *********************/ sbit S_LED1 = P3^4 ; sbit S_LED2 = P3^5 ; sbit S_LED3 = P3^6 ; sbit S_LED4 = P3^7 ; sbit E = P2^2 ; sbit RW = P2^0 ; sbit RS = P2^1 ; sbit dk_LCD = P2^3 ; sbit bit_while = P0^7 ; sbit key_press1 = P2^0 ; sbit key_press2 = P2^1 ; bit bit_program1 = 0x10 ; bit bit_program2 = 0x11 ; bit bit_program3 = 0x12 ; bit bit_program4 = 0x13 ; bit bit_program5 = 0x14 ; bit bit_program6 = 0x15 ; bit bit_program7 = 0x16 ; unsigned char counter ; unsigned char LED ; unsigned short result_converse ; unsigned char LED1 ; unsigned char LED2 ; unsigned char LED3 ; unsigned char LED4 ; unsigned int value_LED ; unsigned char counter_TR ; unsigned char button ; /***************** to define variable *******************/ #define ON0 #define OFF 1 sbit rotate_motor1 = P1^0 ; sbit rotate_motor2 = P1^1 ; sbit rotate_motor3 = P1^2 ; sbit rotate_motor4 = P1^3 ; /* definition speed variable to area */ unsigned char speed_center;

Lp trnh C cho VXL - C bn

bkav huy231086@yahoo.com

unsigned char speed_right_1; unsigned char speed_right_2; unsigned char speed_right_3; unsigned char speed_right_4; unsigned char speed_right_5; unsigned char speed_right_6; unsigned char speed_right_7; unsigned char speed_right_8; unsigned char speed_left_1; unsigned char speed_left_2; unsigned char speed_left_3; unsigned char speed_left_4; unsigned char speed_left_5; unsigned char speed_left_6; unsigned char speed_left_7; unsigned char speed_left_8; /* ________________________________ Go Go Go!!!!!!_________________________*/ void delay(unsigned int value_delay) ; void converse() ; void converse_LED() ; void out_serial( unsigned char value_serial ) ; void LCD_cmd( unsigned char value_LCD_cmd ) ; void LCD_DATA( unsigned char value_LCD_DATA ) ; void READY() ; void LCD_putstring(const unsigned char *str ); void stop_run() ; void main() { unsigned char s1[] = "Hello" ; unsigned char s2[] = "Hello : HUY" ; unsigned char s3[] = "VIET NAM VO DICH" ; unsigned int i ; counter = 0 ; button = 0 ; TMOD = 0x11 ; EA = 1 ; ET0 = 1 ; ET1 = 1 ; EX0 = 1 ; EX1 = 1 ; TF1 = 1 ; TF0 = 1 ; IT0 = 1 ; PT0 = 1 ; value_LED = 1234 ;

Lp trnh C cho VXL - C bn

bkav huy231086@yahoo.com

dk_LCD = 1 ;

Lp trnh C cho VXL - C bn

bkav huy231086@yahoo.com

LCD_cmd(0x01) ; LCD_cmd(0x38) ; LCD_cmd(0x0C) ; LCD_cmd(0x01) ; LCD_cmd(0x80) ; LCD_putstring(s1) ; for (;;) ; } /* interrupt timer 1*/ void interrupt_timer1() interrupt 3 { TR1 = 0 ; TH1 = 0xEE; TL1 = 0x00; TR1 = 1 ; P2 = 0xE7 ; bit_program1 = key_press1 ; P2 = 0xEB ; bit_program2 = key_press1 ; P2 = 0xEF ; bit_program3 = key_press1 ; P2 = 0xF3 ; bit_program4 = key_press1 ; P2 = 0xFB ; bit_program5 = key_press1 ; P2 = 0xFF ; bit_program6 = key_press1 ; P2 = 0xF7 ; bit_program7 = key_press1 ; } /*------------------------------------------------------------------------------------------------------*/ /*==================================================================================== =====================*/ /* Quet LED su dung truyen noi tiep va cac IC giai ma */ void interrupt_timer0() interrupt 1 { unsigned char LED_serial ; TR0 = 0 ; TH0 = 0xEE ; TL0 = 0x00 ; TR0 = 1 ; converse_LED() ; counter_TR++ ; switch (counter_TR) { case 1: LED_serial = LED1 ; LED_serial |= 0xE0 ;

Lp trnh C cho VXL - C bn

bkav huy231086@yahoo.com

out_serial(LED_serial) ; break ; case 2: LED_serial = LED2 ; LED_serial |= 0xD0 ; out_serial(LED_serial) ; break ; case 3: LED_serial = LED3 ; LED_serial |= 0xB0 ; out_serial(LED_serial) ; break ; case 4: LED_serial = LED4 ; LED_serial |= 0x70 ; out_serial(LED_serial) ; counter_TR = 0 ; break ; } } void interrupt_out0() interrupt 0 { if (P3^2 == 0) { ++value_LED ; } else { if (value_LED == 0) goto exit ; --value_LED ; } exit: if ( value_LED == 9999 ) value_LED = 0 ; } void converse() { unsigned int array[] = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x98 } ; result_converse = array[LED] ; } void out_serial( unsigned char value_serial ) { while (!TI) ; TI = 0 ; SBUF = value_serial ; } void converse_LED() { unsigned int phan_du ; LED4 = value_LED / 1000 ; phan_du = value_LED % 1000 ; LED3 = phan_du / 100 ; phan_du %= 100 ; LED2 = phan_du / 10 ; LED1 = phan_du % 10 ; } void LCD_cmd( unsigned char value_LCD_cmd )

Lp trnh C cho VXL - C bn

bkav huy231086@yahoo.com

{ READY() ; P0 = value_LCD_cmd ; RS = 0 ; RW = 0 ; E=1; E=0; } void LCD_DATA(const unsigned char value_LCD_DATA ) { READY() ; P0 = value_LCD_DATA ; RS = 1 ; RW = 0 ; E=1; E=0; } void READY() { bit_while = 1 ; RS = 0 ; RW = 1 ; while ( bit_while ) { E=0; E=1; } } void LCD_putstring(const unsigned char *str ) { while (*str != '\0') { LCD_DATA(*str) ; *str++ ; } } void stop_run() { } void delay(unsigned int value_delay) { unsigned int x; for ( x = 1; x <= value_delay ; x++) continue; } /*____________________________ interrup : reading Encoder _____________________________*/ /*___________________________ interrupt : scan LED ____________________________________________*/