2. cexamples
2.1 watchdog
/**
* @file $rcsfile: twi.c,v $
*
* copyright (c) 2004 atmel.
*
* please read file license.txt for copyright notice.
*
* @brief this file is an example to use watchdog.
*
* this file can be parsed by doxygen for automatic documentation
* generation.
* put here the functional description of this file within the software
* architecture of your program.
*
* @version $revision: 1.0 $ $name: $
*/
/* @section i n c l u d e s */
#include "reg_c51.h"
/**
* function_purpose:this function setup watchdog.
* function_inputs:void
* function_outputs:void
*/
void main(void)
{
/*
selected time-out
mode x1 12 clock periods per peripheral clock cycle.
mode x2 6 clock periods per peripheral clock cycle.
wdtprg|=0x00 (2^14 - 1) machine cycles, 16. 3 ms @ fosca =12 mhz
wdtprg|=0x01 (2^15 - 1) machine cycles, 32.7 ms @ fosca=12 mhz
wdtprg|=0x02 (2^16 - 1) machine cycles, 65. 5 ms @ fosca=12 mhz
wdtprg|=0x03 (2^17 - 1) machine cycles, 131 ms @ fosca=12 mhz
wdtprg|=0x04 (2^18 - 1) machine cycles, 262 ms @ fosca=12 mhz
wdtprg|=0x05 (2^19 - 1) machine cycles, 542 ms @ fosca=12 mhz
wdtprg|=0x06 (2^20 - 1) machine cycles, 1.05 s @ fosca=12 mhz
wdtprg|=0x07 (2^21 - 1) machine cycles, 2.09 s @ fosca=12 mhz
*/
wdtprg|=0x07;/*2.275s @ fosca=11.059200 mhz */
4362a–80c51–07/04
/* watchdog start sequence */
wdtrst=0x1e;
wdtrst=0xe1;
while(1) /* end less */
{
/***********************************************
* program *
************************************************/
/* watchdog reset sequence */
wdtrst=0x1e;
wdtrst=0xe1;
}
}
4362a–80c51–07/04
2.2 keyboard
/**
* @file $rcsfile: keyboard.c,v $
*
* copyright (c) 2004 atmel.
*
* please read file license.txt for copyright notice.
*
* @brief this file is an example to use keyboard.
*
* this file can be parsed by doxygen for automatic documentation
* generation.
* put here the functional description of this file within the software
* architecture of your program.
*
* @version $revision: 1.0 $ $name: $
*/
/* @section i n c l u d e s */
#include "reg_c51.h"
/*_____ d e c l a r a t i o n ___________________________________________*/
unsigned int keypressed; /* key pressed value */
bit key_flag; /* software flag */
/**
* function_purpose:this function setup keyboard.
* a key set idle mode and an other set the power-down mode
* in this two mode the microcontoleur wake up at keyboard event
* function_inputs:void
* function_outputs:void
*/
void main()
{
ea=1; /* enable interrupts */
/* init keyboard */
kbe=0xff; /*enable all p1 i/o as keyboard io */
kbf=0x00; /* clear all keyboard flags */
ien1 |=0x01; /* enable keyboard interupt */
while(1) /* endless */
{
if (key_flag) /* keyboard occur */
{
if ( keypressed==16) /*enter idle mode */
{
/* entering idle mode */
pcon |=0x01;
/* wake up by keypressed value */
}
4362a–80c51–07/04
if ( keypressed==32) /* enter powerdown mode */
{
/* entering powerdown mode */
pcon |=0x02;
/* wake up by keypressed value */
}
key_flag=0; /* reset softaware flag */
}
}
}
/**
* function_purpose:keyboard_interrupt. save pressed key
* function_inputs:void
* function_outputs:void*/
void keyboard_interrupt() interrupt 7 using 1
{
keypressed=kbf; /* save pressed key */
key_flag=1; /* set the software flag */
kbf=0x00; /* clear keyboard flags */
}
4362a–80c51–07/04
2.3 sfr register definition
/*h*************************************************************************
**
* name: at89c51xd2.h
*---------------------------------------------------------------------------
-
* purpose: sfr description file for at89c51xd2 products
* on keil compiler
****************************************************************************
*/
#define sfr(x, y) sfr x = y
#define sbit(x, y, z) sbit x = y^z
#define sfr16(x,y) sfr16 x = y
/*----------------------------------------*/
/* include file for 8051 sfr definitions */
/*----------------------------------------*/
/* byte register */
sfr (p0 , 0x80);
sbit (p0_7 , 0x80, 7);
sbit (p0_6 , 0x80, 6);
sbit (p0_5 , 0x80, 5);
sbit (p0_4 , 0x80, 4);
sbit (p0_3 , 0x80, 3);
sbit (p0_2 , 0x80, 2);
sbit (p0_1 , 0x80, 1);
sbit (p0_0 , 0x80, 0);
sfr (p1 , 0x90);
sbit (p1_7 , 0x90, 7);
sbit (p1_6 , 0x90, 6);
sbit (p1_5 , 0x90, 5);
sbit (p1_4 , 0x90, 4);
sbit (p1_3 , 0x90, 3);
sbit (p1_2 , 0x90, 2);
sbit (p1_1 , 0x90, 1);
sbit (p1_0 , 0x90, 0);
sfr (p2 , 0xa0);
sbit (p2_7 , 0xa0, 7);
sbit (p2_6 , 0xa0, 6);
sbit (p2_5 , 0xa0, 5);
sbit (p2_4 , 0xa0, 4);
sbit (p2_3 , 0xa0, 3);
4362a–80c51–07/04
sbit (p2_2 , 0xa0, 2);
sbit (p2_1 , 0xa0, 1);
sbit (p2_0 , 0xa0, 0);
sfr (p3 , 0xb0);
sbit (p3_7 , 0xb0, 7);
sbit (p3_6 , 0xb0, 6);
sbit (p3_5 , 0xb0, 5);
sbit (p3_4 , 0xb0, 4);
sbit (p3_3 , 0xb0, 3);
sbit (p3_2 , 0xb0, 2);
sbit (p3_1 , 0xb0, 1);
sbit (p3_0 , 0xb0, 0);
sbit (rd , 0xb0, 7);
sbit (wr , 0xb0, 6);
sbit (t1 , 0xb0, 5);
sbit (t0 , 0xb0, 4);
sbit (int1 , 0xb0, 3);
sbit (int0 , 0xb0, 2);
sbit (txd , 0xb0, 1);
sbit (rxd , 0xb0, 0);
sfr (p4 , 0xc0);
sbit (p4_7 , 0xc0, 7);
sbit (p4_6 , 0xc0, 6);
sbit (p4_5 , 0xc0, 5);
sbit (p4_4 , 0xc0, 4);
sbit (p4_3 , 0xc0, 3);
sbit (p4_2 , 0xc0, 2);
sbit (p4_1 , 0xc0, 1);
sbit (p4_0 , 0xc0, 0);
sfr (p5 , 0xe8);
sbit (p5_7 , 0xe8, 7);
sbit (p5_6 , 0xe8, 6);
sbit (p5_5 , 0xe8, 5);
sbit (p5_4 , 0xe8, 4);
sbit (p5_3 , 0xe8, 3);
sbit (p5_2 , 0xe8, 2);
sbit (p5_1 , 0xe8, 1);
sbit (p5_0 , 0xe8, 0);
sfr (psw , 0xd0);
sbit (cy , 0xd0 , 7);
sbit (ac , 0xd0 , 6);
4362a–80c51–07/04
sbit (f0 , 0xd0 , 5);
sbit (rs1 , 0xd0 , 4);
sbit (rs0 , 0xd0 , 3);
sbit (ov , 0xd0 , 2);
sbit (ud , 0xd0 , 1);
sbit (p , 0xd0 , 0);
sfr (acc , 0xe0);
sfr (b , 0xf0);
sfr (sp , 0x81);
sfr (dpl , 0x82);
sfr (dph , 0x83);
sfr (pcon , 0x87);
sfr (ckcon0 , 0x8f);
sfr (ckcon1 , 0xaf);
/*------------------ timers registers ---------------------*/
sfr (tcon , 0x88);
sbit (tf1 , 0x88, 7);
sbit (tr1 , 0x88, 6);
sbit (tf0 , 0x88, 5);
sbit (tr0 , 0x88, 4);
sbit (ie1 , 0x88, 3);
sbit (it1 , 0x88, 2);
sbit (ie0 , 0x88, 1);
sbit (it0 , 0x88, 0);
sfr (tmod , 0x89);
sfr (t2con , 0xc8);
sbit (tf2 , 0xc8, 7);
sbit (exf2 , 0xc8, 6);
sbit (rclk , 0xc8, 5);
sbit (tclk , 0xc8, 4);
sbit (exen2 , 0xc8, 3);
sbit (tr2 , 0xc8, 2);
sbit (c_t2 , 0xc8, 1);
sbit (cp_rl2, 0xc8, 0);
sfr (t2mod , 0xc9);
sfr (tl0 , 0x8a);
sfr (tl1 , 0x8b);
sfr (tl2 , 0xcc);
sfr (th0 , 0x8c);
sfr (th1 , 0x8d);
sfr (th2 , 0xcd);
sfr (rcap2l , 0xca);
sfr (rcap2h , 0xcb);
sfr (wdtrst , 0xa6);
4362a–80c51–07/04
sfr (wdtprg , 0xa7);
/*------------------- uart registers ------------------------*/
sfr (scon , 0x98);
sbit (sm0 , 0x98, 7);
sbit (fe , 0x98, 7);
sbit (sm1 , 0x98, 6);
sbit (sm2 , 0x98, 5);
sbit (ren , 0x98, 4);
sbit (tb8 , 0x98, 3);
sbit (rb8 , 0x98, 2);
sbit (ti , 0x98, 1);
sbit (ri , 0x98, 0);
sfr (sbuf , 0x99);
sfr (saden , 0xb9);
sfr (saddr , 0xa9);
/*-------------------- internal baud rate generator --------*/
sfr (brl , 0x9a);
sfr (bdrcon , 0x9b);
/*-------------------- it registers -----------------------*/
sfr (ien0 , 0xa8);
sfr (ien1 , 0xb1);
sfr (iph0 , 0xb7);
sfr (iph1 , 0xb3);
sfr (ipl0 , 0xb8);
sfr (ipl1 , 0xb2);
/* ien0 */
sbit (ea , 0xa8, 7);
sbit (ec , 0xa8, 6);
sbit (et2 , 0xa8, 5);
sbit (es , 0xa8, 4);
sbit (et1 , 0xa8, 3);
sbit (ex1 , 0xa8, 2);
sbit (et0 , 0xa8, 1);
sbit (ex0 , 0xa8, 0);
/*--------------------- pca registers -----------------------------*/
sfr (ccon , 0xd8);
sfr (cmod , 0xd9);
sfr (ch , 0xf9);
4362a–80c51–07/04
sfr (cl , 0xe9);
sfr (ccap0h , 0xfa);
sfr (ccap0l , 0xea);
sfr (ccapm0 , 0xda);
sfr (ccap1h , 0xfb);
sfr (ccap1l , 0xeb);
sfr (ccapm1 , 0xdb);
sfr (ccap2h , 0xfc);
sfr (ccap2l , 0xec);
sfr (ccapm2 , 0xdc);
sfr (ccap3h , 0xfd);
sfr (ccap3l , 0xed);
sfr (ccapm3 , 0xdd);
sfr (ccap4h , 0xfe);
sfr (ccap4l , 0xee);
sfr (ccapm4 , 0xde);
/* ccon */
sbit (cf , 0xd8, 7);
sbit (cr , 0xd8, 6);
sbit (ccf4 , 0xd8, 4);
sbit (ccf3 , 0xd8, 3);
sbit (ccf2 , 0xd8, 2);
sbit (ccf1 , 0xd8, 1);
sbit (ccf0 , 0xd8, 0);
/*------------------ t w i registers ------------------------------*/
sfr ( sscon , 0x93);
sfr ( sscs , 0x94);
sfr ( ssdat , 0x95);
sfr ( ssadr , 0x96);
sfr ( pi2, 0xf8);
sbit (pi2_1 , 0xf8, 1);
sbit (pi2_0 , 0xf8, 0);
/*-------------------- osc control registers ----------------------*/
sfr ( cksel , 0x85 );
sfr ( osccon , 0x86 );
sfr ( ckrl , 0x97 );
/*-------------------- keyboard control registers -----------------*/
sfr ( kbls , 0x9c );
sfr ( kbe , 0x9d );
sfr ( kbf , 0x9e );
/*-------------------- spi ---------------------- -----------------*/
sfr ( spcon, 0xc3 );
sfr ( spsta, 0xc4 );
sfr ( spdat, 0xc5 );
4362a–80c51–07/04
/*------ misc ----------------------------------------------------*/
sfr( auxr , 0x8e);
sfr ( auxr1, 0xa2);
sfr ( fcon, 0xd1);
/*------ e data --------------------------------------------------*/
sfr ( eecon, 0xd2 );
4362a–80c51–07/04
3. assembler 51 examples
3.1 watchdog
$include (reg_c51.inc)
org 000h
ljmp begin
;/**
; * function_purpose:this function setup watchdog.
; * function_inputs:void
; * function_outputs:void
; */
org 0100h
begin:
;selected time-out
;
;mode x1 12 clock periods per peripheral clock cycle.
;mode x2 6 clock periods per peripheral clock cycle.
;
;orl wdtprg,#00h (2^14 - 1) machine cycles, 16. 3 ms @ fosca =12 mhz
;orl wdtprg,#01h (2^15 - 1) machine cycles, 32.7 ms @ fosca=12 mhz
;orl wdtprg,#02h (2^16 - 1) machine cycles, 65. 5 ms @ fosca=12 mhz
;orl wdtprg,#03h (2^17 - 1) machine cycles, 131 ms @ fosca=12 mhz
;orl wdtprg,#04h (2^18 - 1) machine cycles, 262 ms @ fosca=12 mhz
;orl wdtprg,#05h (2^19 - 1) machine cycles, 542 ms @ fosca=12 mhz
;orl wdtprg,#06h (2^20 - 1) machine cycles, 1.05 s @ fosca=12 mhz
;orl wdtprg,#07h (2^21 - 1) machine cycles, 2.09 s @ fosca=12 mhz
orl wdtprg,#07;/*2.275s @ fosca=11.059200 mhz */
;/* watchdog start sequence */
mov wdtrst,#1eh;
mov wdtrst,#0e1h;
loop: /* end less */
;/***********************************************
;* program *
;************************************************/
; /* watchdog reset sequence */
mov wdtrst,#1eh;
mov wdtrst,#0e1h;
jmp loop
end
4362a–80c51–07/04
3.2 keyboard
$include (reg_c51.inc)
;/*_____ d e c l a r a t i o n ___________________________________________*/
keypressed data 10h; /* key pressed value */
key_flag bit 20h; /* software flag */
org 000h
ljmp begin
org 3bh
ljmp keyboard_it
;/**
; * function_purpose:this function setup keyboard.
; * a key set idle mode and an other set the power-down mode
; * in this two mode the microcontoleur wake up at keyboard event
; * function_inputs:void
; * function_outputs:void
; */
org 0100h
begin:
setb ea; /* enable interrupts */
;/* init keyboard */
mov kbe,#0ffh; /*enable all p1 i/o as keyboard io */
mov kbf,#00; /* clear all keyboard flags */
orl ien1,#01h; /* enable keyboard interupt */
loop:
jnb key_flag,end_if_key_flag /* keyboard occur */
mov r7, keypressed;
cjne r7,#10h,end_if_key0x10; /*enter idle mode */
; /* entering idle mode */
orl pcon,#01h;
; /* wake up by keypressed value */
end_if_key0x10:
cjne r7,#20h,end_if_key0x20; /* enter powerdown mode */
; /* entering powerdown mode */
orl pcon,#02h;
; /* wake up by keypressed value */
end_if_key0x20:
clr key_flag; /* reset softaware flag */
end_if_key_flag:
jmp loop
4362a–80c51–07/04
;/**
;* function_purpose:keyboard_interrupt. save pressed key
;* function_inputs:void
;* function_outputs:void
;*/
keyboard_it:
mov keypressed,kbf; /* save pressed key */
setb key_flag; /* set the software flag */
mov kbf,#00h; /* clear keyboard flags */
reti
end
4362a–80c51–07/04
3.3 sfr register definition
$save
$nolist
p0 data 80h
tcondata88h
;---tcon bits ---
tf1 bit 8fh
tr1 bit 8eh
tf0 bit 8dh
tr0 bit 8ch
ie1 bit 8bh
it1 bit 8ah
ie0 bit 89h
it0 bit 88h
p1 data 90h
scon data 98h
;--- scon bits ----
sm0 bit 9fh
sm1 bit 9eh
sm2 bit 9dh
ren bit 9ch
tb8 bit 9bh
rb8 bit 9ah
ti bit 99h
ri bit 98h
p2 data 0a0h
ien0 data 0a8h
;--- ien0 bits -----
ea bit0afh
ec bit0aeh
et2 bit0adh
es bit0ach
et1 bit0abh
ex1 bit0aah
et0 bit0a9h
ex0bit0a8h
p3 data 0b0h
;--- p3 bits -------
rd bit 0b7h
wr bit 0b6h
t1 bit 0b5h
t0 bit 0b4h
int1 bit 0b3h
4362a–80c51–07/04
int0 bit 0b2h
txd bit 0b1h
rxd bit 0b0h
p4 data 0c0h
p5 data 0e8h
ipl0data0b8h
;--- ipl0 bits -----
ppcl bit0beh
pt2l bit0bdh
psl bit0bch
pt1l bit0bbh
px1l bit0bah
pt0l bit0b9h
px0lbit0b8h
t2con data 0c8h
;--- t2con bits ----
tf2 bit 0cfh
exf2 bit 0ceh
rclk bit 0cdh
tclk bit 0cch
exen2 bit 0cbh
tr2 bit 0cah
c_t2 bit 0c9h
cp_rl2 bit 0c8h
psw data 0d0h
;--- psw bits ------
cy bit 0d7h
ac bit 0d6h
f0 bit 0d5h
rs1 bit 0d4h
rs0 bit 0d3h
ov bit 0d2h
p bit 0d0h
ccondata0d8h
;--- ccon bits -----
cf
cr
ccf4
ccf3
ccf2
ccf1
ccf0
acc
bit
bit
bit
bit
bit
bit
bit
data
0dfh
0deh
0dch
0dbh
0dah
0d9h
0d8h
0e0h
4362a–80c51–07/04
b data
sp data
dpl data
dph data
pcon data
tmod data
tl0 data
tl1 data
th0 data
th1 data
auxrdata08eh
ckcon0data08fh
sbuf data
0f0h
81h
82h
83h
87h
89h
8ah
8bh
8ch
8dh
99h
;-- baud rate generator
brl data09ah
bdrcon data 09bh
;--- keyboard
kblsdata09ch
kbedata09dh
kbfdata09eh
;--- watchdog timer
wdtrstdata0a6h
wdtprg data0a7h
saddrdata0a9h
ckcon1data0afh
ien1data0b1h
ipl1data0b2h
iph1data0b3h
iph0data0b7h
sadendata0b9h
t2moddata 0c9h
rcap2l data 0cah
rcap2h data 0cbh
tl2 data 0cch
th2 data 0cdh
cmoddata0d9h
4362a–80c51–07/04
ccapm0data0dah
ccapm1data0dbh
ccapm2data0dch
ccapm3data0ddh
ccapm4data0deh
chdata0f9h
ccap0hdata0fah
ccap1hdata0fbh
ccap2hdata0fch
ccap3hdata0fdh
ccap4hdata0feh
cldata0e9h
ccap0ldata0eah
ccap1ldata0ebh
ccap2ldata0ech
ccap3ldata0edh
ccap4ldata0eeh
; spi
spcon data 0c3h
spsta data 0c4h
spdat data 0c5h
; twi
pi2data 0f8h
sscondata093h
sscsdata094h
ssdatdata095h
ssadrdata096h
pi2_obit0f8h
pi2_1bit0f9h
; clock control
osccondata086h
ckseldata085h
ckrldata097h
;misc
auxr1data0a2h
; flash control
fcon data 0d1h
;eedata
eecondata0d2h
$restore
4362a–80c51–07/04