Вы находитесь на странице: 1из 19

Programming C

AVRStudio4 > New Project > Project Type: AVR GCC, Project Name: Test1, Yes, Create a i itia! "i!e > Ne#t > $e%u& P!at"orm: AVR Simu!ator, $e'ice: ATme&a( > )i is* +a'e a !oo,, ri&*t c!ic, o your Project-s Tree > .dit Co "i&uratio /ptio s0 1 w*ic* ta% are t*e &cc optio s2 +a'e a ot*er !oo, 3ui!d > .#port 4a,e"i!e, sa'e it i t*e wor,i & directory, ri&*t c!ic, o your project-s /t*er )i!es > Add .#isti & )i!e a d add t*e ma,e"i!e you just e#ported0 T*is is t*e compi!er-s co "i&uratio "i!e "or t*is project, do -t %ot*er "or ow0 +a'e a &ood !oo, at t*e 15/ 'iew, rea!!y eat *u*2 Now, copy5paste to your test10c a d 3ui!d 6
int main(void) { while (1) { } } // start // repeat nothing to infinity

T*ere is a stro & possi%i!ity t*at you wi!! &et a 7ar i &, o worries0 8oo, at t*e output0 C*a &e somet*i &0 Recompi!e0 Now you are trapped, you are &oi & to do it t*ousa ds o" times a&ai 9:;0 A ot*er t*i & you may wa t to see is View > $isassem%!er0 T*is is t*e ATme&a( pro&ram memory a d t*e assem%!y i structio s t*at are &oi & to %e e#ecuted0 Your micro does ot rea!!y u dersta d C, we do0

LED ON
/<, 8et-s ma,e somet*i & *appe , t*is 8.$ *as to tur o 0 1-!! put it simp!y, e'e t*e &reatest project o" a!! times *as to *a'e a 8.$ doi & somet*i & a d most !i,e!y it started "rom t*at0 7as it = or 1 "or o 2 = 1 t*i ,0 / w*ic* pi is it2 +a'e a !oo, at t*e sc*ematic0 +ow do 1 tur it o 2 7e!!, just dec!are t*is port pi to %e a output a d set it to =0 +a'e a !oo, at t*e datas*eet, you wi!! "i d a e#amp!e0 +ere is *ow it &oes:
#include <avr/io.h> int main(void) { ""#$ % &''( )*#+$ % ,( } return (,)( // header file ... for all register names to get their meaning // program starts here we don!t even need our super loop

// set port $ for output yes 1 is for output // set all port $ pins to , // you have to return something if you don!t li-e the warning

Compi!e : $ow !oad6 +as t*e 8.$ tur ed o 2 Great6 No2 You press 3ac, t*ree times, %y t*e way is t*e ri&*t code ru i & i to t*e micro2 Now, t*is code a""ected a!! Port 3 pi s, ot just t*e pi t*e 8.$ is o , ot ice0 Ca you rewrite it ot to2 Come o 0 Not to "or&et, did you c*ec, at your project-s tree t*e e#ter a! depe de cies2 7*ere are t*e re&isters dec!ared2 +a'e a !oo, at t*e datas*eet, ca you e a%!e t*e i ter a! pu!!:up2 You supp!y more curre t ow, ca you otice a y %ri&*t ess di""ere ce2

LED ON Delay, LED OFF Delay


/<, o,, we ca add a de!ay0 /N $e!ay /)) $e!ay, /N $e!ay /)) $e!ay, 000
#include <avr/io.h> /// +ypedefs ////////// typedef unsigned char u.( typedef unsigned int u1/( typedef unsigned long u0&( /// "efines /////////// #define forever 111 #define 23"*44 )*#+$ 5% (1<<6) #define 23"*7 )*#+$ 8% 9(1<<6) /// )rototypes //////// void :nit)orts (void)( void "elay(u0& count)( int main(void) { :nit)orts()( while (forever) { 23"*7( "elay(&,,,,)( 23"*44( "elay(&,,,,)( }

void :nit)orts(void) { ""#$ 5% 1<<""$6( } void "elay(u0& count) { while(count;;)( }

Compi!e : $ow !oad6 You are *appy, are -t you2 .#perime t wit* di""ere t de!ays, ma,e de!ay 'ary, ca you re!ate $e!ay 'a!ues wit* seco ds2 You wi!! eed to simu!ate your code wit* AVRStudio, remem%er to set t*e c!oc, speed0 +ow "ast ca your eyes se se %!i ,i &2 7*y 11>2 ::$0 Are t*ese typede"s rea!!y eeded or somew*ere t*ere are simi!ar de"i itio s2 Ca you write &e eric pi set5reset macros to a'oid co ti uous %it twea,i & a d ma,e your code more reada%!e2

LED and Button


7e p!ayed wit* t*e 8.$, !et-s p!ay wit* t*e %utto , it-s pretty easy0 You ca ,eep our pre'ious code a d just c*a &e t*e mai routi e wit* t*e "o!!owi & o e0 Read it "irst a d t*i ,, ru it "irst i your mi d a d t*e i your micro0 7*at do you e#pect to see a"ter Reset2
int main(void) { u. <tn=tate( :nit)orts()( 23"*44( while (forever) { <tn=tate % 9):7$ 8 (1<<0)( if (<tn=tate)

// <utton pin reading

{ } } }

23"*7( "elay(',,,)( 23"*44( "elay(',,,)(

1 to!d you, it is easy0 T*e o !y di""ere ce is t*at you read P1N3, i stead o" writti & to P/RT30 ? dersta d t*e !o&ic %e*i d t*e %utto pi readi &0 Cou!d t*is comma d reside outside t*e !oop2 8ast t*i &, c*a &e t*e a%o'e code to ma,e t*e 8.$ c*a &e state o %utto re!ease0 1 c*a!!e &e you0

7Segment
You , ow *ow t*e 8.$ wor,s, ri&*t2 1" you mu!tip!y w*at you , ow %y (, you &et a > se&me t disp!ay wit* a dot aside0 Various co!ours a d si@es0 No di""ere ce0 T*ere is o !y o e t*i & to remem%er *ere, a!! 8.$s i a >:se& disp!ay *a'e eit*er t*eir positi'e or e&ati'e !e&s tied to&et*er0 T*e Atied !e&sA are to %e co ected eit*er to BV or GN$0 / t*e ot*er side o" eac* 8.$ you *a'e to p!ace a resistor0 T*e o es we -'e &ot are commo a ode 'ersio , a!! positi'e 8.$ !e&s are co ected to&et*er at pi s 1 a d C0 A&ai , we eed DV across eac* 8.$ a d a%out 1=mA0 You may !oo, at t*e sc*ematic a d t*e datas*eet0 Now, w*ic* pi is w*ic* 8.$ 000

As you ima&i ed, w*e you &rou d o e o" t*e pi s t*rou&* a resistor, you wi!! see t*e correspo di & 8.$ tur ed o 0 You see t*e story, "or eac* di&it we *a'e to decide w*ic* 8.$s to tur o 0 You may "i d t*e "o!!owi & de"i itio s *a dy0

// 1=3> #define #define #define #define #define #define #define #define #define #define #define #define #define #define // 23" #define #define

a, <, c, d, e, f, g, a1 <1 c1 d1 e1 f1 g1 ledoff ledon

)*#+$ )*#+$ )*#+? )*#+" )*#+" )*#+$ )*#+" )*#+? )*#+? )*#+" )*#+" )*#+? )*#+? )*#+"

8% 8% 8% 8% 8% 8% 8% 8% 8% 8% 8% 8% 8% 8%

9(1<<&) 9(1<<1) 9(1<<,) 9(1<<') 9(1<</) 9(1<<,) 9(1<<1) 9(1<<6) 9(1<<') 9(1<<&) 9(1<<6) 9(1<<1) 9(1<<&) 9(1<<0)

)*#+$ 5% (1<<6) )*#+$ 8% 9(1<<6)

Serial Debugging Interface


7*e 1 "irst t*ou&*t o" %ui!di & a sma!! ,it a d se!!i & it out "or educatio a! purposes, t*e seria! i ter"ace was ot i t*e p!a s0 1 t*ou&*t, yes its pretty muc* sta dard, %ut w*y to add t*e e#tra cost2 1t mi&*t %e a ice t*i & to p!ay wit*, %ut is it rea!!y eeded2 T*e , t*e time came "or me to p!ay wit* t*e 1: wire routi es a d 1 "ou d myse!" doi & a !ot o" de%u&&i & %y se di & stu"" to t*e seria! port at ru time0 So, 1 did -t wa t to %e u "air, t*e seria! i ter"ace is ice to use "or ru time de%u&&i &0 1" you do -t catc* w*at 1 am ta!,i & a%out, t*i , w*at A*i&*:!e'e!A peop!e do, t*ey add %rea,poi ts to t*eir code a d t*ey i spect 'aria%!es0 7e wi!! do t*e same, we wi!! se d 'aria%!es "rom our micro to t*e PC-s seria! port a d i spect t*em wit* a termi a! at ru time0 1dea!!y, we s*ou!d a!ways , ow at desi& time w*at our code wi!! do at ru time, u "ortu ate!y sometimes t*is is ot t*e case0 A &ood t*i &, ow wit* t*e seria! i ter"ace, we mi&*t a!so write a PC temperature !o&&i & uti!ity0 )or t*e seria! port t*ere are a 'ast amou t o" resources o t*e et, 1 -!! &i'e a 'ery Euic, descriptio 0 3ot* AT4e&a( a d a sta dard PC *a'e a ?SART, t*ese modu!es *a d!e commu icatio 0 T*ey are respo si%!e "or ta,i & a %yte "rom a re&ister, "ormati & it a d tra smitti & it seria!!y0 At t*e same time t*ey do t*e receptio as we!!, w*e a %yte is recei'ed seria!!y t*ey p!ace it o a re&ister0 T*ere are two wires "or t*is story so, commu icatio is "u!!:dup!e#, tra smissio a d receptio "or a party *appe simu!ta eous!y0 Two t*i &s to remem%er *ere, "irst, t*e 'o!ta&e !e'e!s "or t*e 1s a d =s, at t*e ca%!e, are ot BV a d =V, t*ey are a%out 1BV a d :1BV, t*us t*e c*ip 4AFDGD does t*e 'o!ta&e !e'e! co 'ersio 0 T*ese 'o!ta&e !e'e!s e sure %etter commu icatio at !o & dista ces0 +a'e a !oo, at t*e sc*ematic a d t*e datas*eet o" 4AFDGD0 Notice t*at T# a d R# are crossed0 T*e seco d, pac,et "ormat a d tra smissio %it rate *a'e to %e a&reed %etwee t*e two parties, i" ot, e#pect "ai!ure0 +a'e a !oo, at t*e ?SART modu!e i t*e datas*eet0 /ur c!oc, is >0G>D(4+@, *ow 1 it?SART s*ou!d %e ca!!ed2
void :nit@=A#+ (u1/ <aud) { @$##B % (u.)(<aud>>.)( @$##2 % (u.)<aud( @?=#$ % (1<<#C37)5(1<<+C37)( @?=#? % (1<<@#=32)5(1<<@?=D1)5(1<<@?=D,)( }

// // // //

=et $aud rate ; ?ast Bigh <yte =et $aud rate ; ?ast 2ow <yte 3na<le #eceiver 8 +ransmitter , parity 1 stop <it

)rom t*e datas*eet we ca &et t*e Tra smit3yte a d Recei'e3yte routi es0 +owe'er, , ow your code0
void +E$yte (u. data) { while ( F( @?=#A 8 (1<<@"#3)) )( @"# % data( forces transmission } u. #E$yte (void) { while ( F(@?=#A 8 (1<<#C?)) )( return @"#( }

// Gait for empty transmit <uffer // )utting data into the <uffer

// Gait for data to <e received // #eturn "ata

P!ease, t*i , o" some tests a d per"orm t*em0

Serial Loop Back Test


/ e o" t*e mora!s 1 !i,e is ANot*i & wor,s, u ti!! it is testedA0 +a'e you ma a&ed to se d %ytes o'er t*e seria! i ter"ace2 7e!!, !et-s do a commo test, t*e !oop%ac, test0 7e wi!! se d %ytes "rom our PC to our micro a d our micro is e#pected to se d t*e %ytes %ac,0
:nit@=A#+(&0)( while (forever) { +E$yte(#E$yte()J1)( } // :nitialiHe @=A#+ 1I&,, <aud

// =end <ac- the neEt ascii

A"ter t*e code is dow , p!ace your seria! ca%!e, ope *ypertermi a!, w*ate'er C/4 you are usi &, 1HD== %its per seco d, ( data %its, o parity, 1 stop %it a d o "!ow co tro!0 /* my God, my ,ey%oard *as &o e mad0 Tip "or t*e !ate *ours, i" you wa t to pro&ram your %oard a&ai a d you *a'e a si &!e seria! port, you wi!! *a'e to disco ect t*e termi a! "rom t*e seria! port a d p!ace t*e ca%!e %ac, i to your pro&rammer0 Now, say you *ad o 8.$s o your %oard a d you wa ted to test t*e state o" t*e %utto , w*at wou!d you do2 You cou!d ,eep readi &, i t*e super !oop, t*e state o" t*e %utto a d se d a c*aracter to your PC i dicati & state0 Say AAA "or pressed a d A3A "or ot pressed0 T*e ascii ta%!e mi&*t %e *a dy0 Simp!e2 P!ease try it0 Not to "or&et, add a desce t de!ay i t*e !oop, &et a appreciatio o" w*at a desce t de!ay s*ou!d %e *ere0 7*at i" you do -t put a de!ay at a!!2 You wou!d se d %ytes to your PC too "ast0 +as your PC cras*ed yet2 *e*e*e*e your /S is ot 'ery c!e'er *u*2 / e t*i , to poi t out *ere is t*at t*ese routi es are ot 'ery i te!!i&e t0 7e *a'e to %e care"u!! w*e we use t*em, say "or e#amp!e t*at you are ru i & a !o & de!ay a d at t*e same time %ytes come "rom t*e PC0 7*at wi!! *appe 2 T*ese %ytes wi!! %e dropped, our micro wi!! ,eep !oopi & i t*e de!ay a d wi!! ot *a d!e commu icatio 0 So, a'oid %yte %ursts "or ow, %ot* sides0 1 case you are wo deri &, t*e %est way to *a d!e commu icatio is wit* i terrupts0 Asy c*ro ous!y wit* t*e pro&ram "!ow, %ytes may %e se t or recei'ed0 1" you wa t to &et i to it !oo, at t*e AVR app0 otes0 7e wi!! !ea'e it "or ow cause we *a'e w*at we eed0

Big Picture
You are &etti & i to it, &et out "or a sec, !ets rea!i@e *ow de'e!opme t wi!! &o o 0 7e *a'e a idea o" t*e too!s, t*eir usa&e0 7e ca use t*em0 T*e %oard set up is tested, we ca dow !oad code a d see it wor,i &0 7e ca co tro! port pi s0 8.$s, %utto s, w*ate'er0 3ui!di & o t*at we ca co tro! t*e >:se&me t disp!ay0 7e *a'e a way o" testi & our code at ru time0 3ui!di & o t*at we ca write a *i&* !e'e! routi e t*at disp!ays um%ers0 T*is o e s*ou!d *ide a!! !ow !e'e! comp!e#ity a d s*ou!d %e ca!!ed !i,e t*is:
"isplay (num)(

A &ood poi t wou!d %e writi & a A"orA !oop "or cou ti & "rom = to HH0 1" we e#te d t*is %y addi & t*e mi us sym%o!, we ca disp!ay as !ow as :HC0 +mm, or we may use t*e e#tra 8.$ "or si& i dicatio 0 7*e we are *appy wit* our routi e, we "or&et t*at a d we start readi & t*e 1:wire protoco!0 No p!a s "or t*e 1:wire ow 000 "irst t*e readi &, t*e t*e p!a s0 A"ter ma,i & it wor, we write a ot*er *i&* !e'e! routi e t*at wou!d %e ca!!ed !i,e t*is:
temp % #ead+emp()(

A&ai t*is o e s*ou!d *ide t*e protoco! mess0 7it* t*ese two routi es !i"e wi!! %e easier "or us, o 3us* wi!! stay a %it more %ut, we wi!! %e a%!e to a'era&e temperature o'er time, co 'ert Ce!cius to )a*re ei&*t wit* t*e c!ic, o" a %utto , etc0 1 *a'e a "ew more ideas, *mmm, or 000 we!!, !et-s &et t*ere "irst0

T e Display
Good, 1 am sure you -'e p!ayed wit* 8.$s so "ar a d you appreciate t*e eed "or a si &!e *i&*:!e'e! routi e0 1 t*i ,, it is %est to use t*e e#tra !ed as a si& i dicator0 +ere "o!!ows my attempt "or t*is routi e0
void "isp#ight (u. digit) { switch (digit) { case , K a1( <1( c1( d1( e1( f1( <rea-( case 1 K <1( c1( <rea-( case & K a1( <1( d1( e1( g1( <rea-( case 0 K a1( <1( c1( d1( g1( <rea-( case 6 K <1( c1( f1( g1( <rea-( case ' K a1( c1( d1( f1( g1( <rea-( case / K a1( c1( d1( e1( f1( g1( <rea-( case 1 K a1( <1( c1( <rea-( case . K a1( <1( c1( d1( e1( f1( g1( <rea-( case I K a1( <1( c1( f1( g1( <rea-( default } } K <rea-(

void "isp2eft (u. digit) { switch (digit) { case , K <rea-( it case 1 K <,( c,( case & K a,( <,( case 0 K a,( <,( case 6 K <,( c,( case ' K a,( c,( case / K a,( c,( case 1 K a,( <,( case . K a,( <,( case I K a,( <,( default K <rea-( } }

// if , is left <rea-( d,( e,( g,( <rea-( c,( d,( g,( <rea-( f,( g,( <rea-( d,( f,( g,( <rea-( d,( e,( f,( g,( <rea-( c,( <rea-( c,( d,( e,( f,( g,( <rea-( c,( f,( g,( <rea-(

no need to display

void "isplay (s. num) { s. dec uni(

)*#+$ 5% ,E11( )*#+? 5% ,E01( )*#+" 5% ,E4?( if (num>;1,, 88 num<1,,) { if (num<,) { ledon( num%;num( } dec % num/1,( uni % numM1,( "isp2eft (dec)( "isp#ight (uni)( "elay (',,,,)( } }

// all leds off

// if negative Lust switch 23" on

T*e "irst two routi es are ide tica!, eac* routi e is co tro!i & t*e respecti'e >: se&me t disp!ay0 7e do -t rea!!y eed to disp!ay =s at t*e decades di&it0 As 1 said a!! we eed is to decide w*ic* 8.$s to switc* o "or eac* di&it0 Now, t*e $isp!ay routi e starts %y c!eari & o"" a!! 8.$s, it c*ec,s i" t*e i put um%er is wit*i accepta%!e !imits a d disp!ays it0 1t just &ets t*e decades a d u its di&its a d i 'o,es t*e respecti'e routi es0 1" t*e um%er is e&ati'e, t*e si& 8.$ is switc*ed o a d t*e e&ati'e si& is remo'ed0 .'e tua!!y, t*ere is a de!ay "or us *um%!e *uma s to otice t*i &s0 Not to "or&et, *a'e you oticed t*e s( at t*e routi e dec!aratio 2

T e !"#ire, protocol - the thermo


So, we are &oi & to write a piece o" code t*at imp!eme ts t*e 1:wire protoco!0 Start %y &at*eri & i "o, t*e "isrt t*i & to !oo, at is t*e $S1(3D= datas*eet, &et a "ee!i & o" w*at is a%out0 No eed to u dersta d e'eryt*i & i t*e datas*eet0 / 4a#im-s site t*ere are to es o" app!icatio otes, 1 wou!d su&&est app!icatio otes 1CD,1(>,1DC,1D=, wit* t*is seEue ce, pro%a%!y "irst AVRG1( app!icatio ote w*ic* is easy to "o!!ow0 Come o , s*ow a a&&ressi'e attitude, a!t*ou&* you do -t u dersta d e'eryt*i &, 1 do -t eit*er0 $o ot copy5paste pieces o" code *opi & it-s &oi & to wor,, it wi!! ot0 )rom t*e &rou d we &o up0 Try to simp!i"y t*i ,s %y as,i & yourse!" simp!e Euestio s0 You see t*at ma y t*ermos ca %e co ected to&et*er o a %us, a micro ta!,s to t*em, so t*ey *a'e to %e addressed wit* a u iEue 1$ It*i , et*er et J 1P a a!o&y;0 T*ere is a si &!e wire, so o !y o e spea,s at a time0 7*o coordi ates discussio 2 T*e master0 T*e micro I"or&et et*er et a a!o&y;0 7e!!, i" it does *ow t*e *e!! it , ows w*ic* t*ermo is o t*e %us2 T*ere is a a!&orit*m "or doi & t*at, t*e searc* a!&orit*m0 $o 1 care2 No, ot "or t*is project, we *a'e a si &!e t*ermo o t*e %us0 1s t*ere a way to ide ti"y commu icatio errors or ma!"u ctio i & t*ermos2 T*e CRC does t*at0 1" you were a micro, a d wa ted to ta!, o'er your *a!":dup!e# co "ere ce p*o e to your "e!!ow t*ermos, w*at i tero&atio comma ds you wou!d !i,e to *a'e2 T*ere are a "ew simp!e co cepts, t*at reaso a%!y "it i to t*e pu@@!e0 $o -t %ypass t*em0

!"#ire $its
7*ere to start2 T*e "irst t*i , to try a"ter &etti & t*e idea is se di & 1s a d =s0 Readi & 1s a d =s wou!d *e!p too0 1t comes dow to *o!di & t*e pi "or

output a d re!easi & it "or i put0 7*e set "or output, t*e pi ca %e writte , w*e set "or i put t*e pi ca %e read 000 +a'e a !oo, at t*e "o!!owi & de"s a d see i" t*ey ma,e a y se se0
#define #define #define #define #define #define wmasw*ut w:n wB w2 w# ,E,. ""#A 5% wmas""#A 8% 9wmas)*#+A 5% wmas)*#+A 8% 9wmas):7A 8 wmas// // // // // // set set set set set get pin mas1;wire pin 1;wire pin 1;wire <us 1;wire <us 1;wire <us for output for input (<us released) high low input (<us released)

Now, t*is is just pi to&&!i &, "rom t*at we eed to &et to t*e %asic %us si& a!s0 8et-s &o o e !e'e! up0 +mm, wait a mi ute, it !oo,s !i,e we eed a de!ay "irst0 /<0 T*i , o" 000
void u"elay(u. count) { while(count;;)( } // delay % '.1, J (1.&& N count)us

T*is is a de!ay %ut *ow !o & does it !ast2 You eed to simu!ate your code wit* AVRStudio, p!ease do so0 Go to $e%u& > AVR Simu!ator /ptio s > )reEue cyK>0G>D( 4+@0 A"ter compi!i &, $e%u& > Start $e%u&&i &, press )11 a coup!e o" times a d !oo, at t*e stopwatc*0 You may reset your stopwatc* %y ri&*t c!ic,i & o it0 1 *ope you &et t*e same resu!t as 1 did: de!ay K B0>= L 10DD M cou t microseco ds "or a >0G>D(4+@ c!oc,0 / e time s!ot is C=us a d !oo,i & at t*e AVRG1( app0 ote we ca rea!i@e *ow to output 1s a d =s0000
void w+E<it (u. <it) { w*ut(w2( if (<it) { u"elay (6)( w:n( u"elay (6/)( } else { u"elay (6/)( w:n( u"elay (6)( } }

// 1,.'.us // /1..&us

// /1..&us // <etter releash the <us // 1,.'.us

$e!ay co strai s *a'e to %e met %ut, t*e protoco! timi & is ot 'ery strict0 / to t*e read routi e 000
u. w#E<it (void) { u. data( w*ut(w2( u"elay (,)( w:n( u"elay (,)( data%w#( u"elay (&0)( } return data( // 1,.'.us // 1,.'.us // /1..&us

Your time *as come, a"ter c!ear!y u dersta di & t*e a%o'e, you *a'e to imp!eme t t*e Reset5Prese ce si& a!0 8uc,y you6

!"#ire $ytes
+a'i & t*e %its a d &etti & to t*e %ytes s*ou!d %e easy0 Si &!e t*i & to reme%er *ere is commu icatio *appe s !east si& i"ica t %it "irst0 T*is is w*e you *a'e a %yte to se d, you are &oi & to se d %it= "irst a d %it> !ast0 1 wou!d su&&est &i'e it a &o a!o e "irst, do -t !oo,, do -t 000
void w+E<yte (u. data) { u. i( for (i%,( i<.( iJJ) { w+E<it (data 8 ,E,1)( data>>%1( }

)or ( times we mas, t*e 8S3 %it a d we se d it0 .ac* time, we s*i"t "or t*e e#t %it0
u. w#E<yte (void) { u. i <it data( data%,( for (i%,( i<.( iJJ) { <it%w#E<it()( data5%<it<<i( } } return data(

)or ( times we read a %it a d we p!ace it i t*e data %yte0 /**, 1 was a%out to "or&et, i case you are i dou%t a%out your 1:wire Reset routi e 0000
u. #eset1Gire (void) { u. present( w*ut(w2( u"elay (1I&)( u"elay (1I0)( w:n( u"elay (6/)( present%w#( u"elay (1/0)( u"elay (1/6)( } return present( // 6.1.1,us // /1..&us // 61,.06us (following guidelines) // , for no device present

To %e *o est wit* you, t*e Reset17ire routi e is t*e "irst you s*ou!d write cause it &i'es "eed%ac,0 3y just se di & %its o'er your 1:wire it does -t rea!!y te!! you somet*i &0 You ca "or e#amp!e try to tur o t*e 8.$ i case t*ere is a t*ermo prese t0 1" you ma a&e to ma,e t*is o e wor, you wi!! a!so %e sure a%out your timi & ca!cu!atio s0

Commands
8oo,i & at t*is s!ide mea s you *a'e your !ow !e'e! 1:wire stu"" up a d ru i & a d tested0 1" you just copy5pasted p!ease !et me , ow cause 1 am serious!y t*i ,i & o" %u&&i & up my code0 7*ate'er t*e case, its time to do

t*e *i&* !e'e! stu""0 Gi'e it a &o, decide w*at you eed a d t*e &o t*rou&*0 1t !oo,s pretty muc* strai&*t "orward 000 )irst, we eed to Reset our 1:wire de'ice a d wait "or t*e Prese ce si& a!0 A"ter we &et it, we issue a S,ip R/4 comma d, we do -t rea!!y eed to address our s!a'e0 T*e , we ca te!! our t*ermo to capture temperature data, t*is is t*e Co 'ert Temperature comma d0 7e eed a&ai to Reset our 1:wire de'ice a d wait "or t*e Prese ce si& a!0 A"ter we &et it, we issue a&ai a S,ip R/4 comma d to "orse our t*ermo ta!,0 .'e tua!!y, we wi!! reEuest t*e temperature data, t*is is t*e Read RA4 comma d0 7*at we &et is a "rame o" H %ytes, D "or temperature, C "or t*e t*ermo 1$ a d 1 "or t*e cyc!ic redu da cy code0 T*e Co 'ert Temperature a d t*e Read RA4 comma ds *a'e to %e separated, eac* o e *as to %e preceded %y a Reset a d a S,ip R/4 comma d0 To me, t*is was ot c!ear "rom t*e %e&i i &, 1 *ad to do ma y tests0 So 000 te!! me w*at you t*i ,0
/// >lo<als /////////// u. ls<+ ms<+ rom, rom1 rom& rom0 rom6 rom' fcrc( void >et+ (void) { u. temp( temp % #eset1Gire()( "elay(1,)( w+E<yte(s-ip#*O)( w+E<yte(convert+)( "elay(&,,)( temp % #eset1Gire()( "elay(1,)( w+E<yte(s-ip#*O)( w+E<yte(read#AO)( ls<+%w#E<yte()( ms<+%w#E<yte()( rom,%w#E<yte()( rom1%w#E<yte()( rom&%w#E<yte()( rom0%w#E<yte()( rom6%w#E<yte()( rom'%w#E<yte()( fcrc%w#E<yte()( +E$yte(ls<+)( +E$yte(ms<+)( +E$yte(rom,)( +E$yte(rom1)( +E$yte(rom&)( +E$yte(rom0)( +E$yte(rom6)( +E$yte(rom')( +E$yte(fcrc)( // #eset =ignal // =-ip #*O // ?onvert +emperature // #eset =ignal // =-ip #*O // #ead #AO

You s*ou!d , ow %y ow your t*ermo-s u iEue 1$ a d your room-s temperature6 You !i,e it2 1 "ee! your e#citeme t0 7e!!, it does wor,0 3ut, it is a %it crappy0 No, ot crappy just i comp!ete0 A!t*ou&* it wor,s2 Yes, *a'i & somet*i & t*at wor,s is ot e'eryt*i &6 7ou!d you mo itor your uc!ear reactor-s temperature wit* t*is routi e2 No2 7*y ot2 3y t*e way, you mi&*t %e wo deri & i" someo e w*ispered to me t*e $e!ay 'a!ues0 No, 1 did ma y tests, se di & %ytes 'ia t*e seria! port wit* s*orter de!ays0

Debugging, anticipate
3e"ore we re!ease our code we *a'e to a ticipate a!! possi%!e sources o" error0 G?1 code may "ai! misera%!y !eadi & to uisa ce, em%edded code may "ai! misera%!y !eadi & to u ima&i a%!e !osses0 Yes, 1 wa t to terri"y you cause we usua!!y t*i , we *a'e "i is*ed %y t*e time we see somet*i & wor,i &0 7e s*ou!d co sta t!y stri'e towards %etter code0 3y t*e way, 1 remai ope to a y su&&estio s 5 correctio s0 /<, !et-s try to ide ti"y possi%!e errors0 )irst o" a!!, w*e we se d a Reset si& a!, it is ot "or sure t*at our t*ermo wi!! %e prese t0 /ur t*ermo mi&*t %e dead0 A!so, a"ter se di & a comma d to our t*ermo t*ere is a possi%i!ity t*at we wi!! e'er &et a rep!y0 T*i , o" addi & timeouts0 3ut a&ai , e'e i" we &et a temperature measureme t, it mi&*t %e erro eous0 T*e CRC sta ds t*ere "or t*is purpose0 )i a!!y, our code may "ai! wit*out &!ory, u e#pected!y0 4icros do "ai!0 Yes, a asty t*i & t*us, we use t*e 7atc*do& timer0 7*at we are &oi & to do is i troduce error codes0 1 case o e o" t*em appears we ca disp!ay it, say A.DA "or e#amp!e0 At !east, w*e your customers ca!! you wit* madde i & c!aims, te!!i & you t*ere is a A.DA comi & out i stead o" temperature, you wi!! , ow w*ic* routi e *as "ai!ed you0 So 000
/// >lo<als /////////// static volatile u. error%,( static u. #AOPIQ( /// )rototypes //////// u. >et+(u. Ndata)( int main(void) { //{...} error % >et+(#AO)( if (Ferror) "isp+emp(temp)( else "isp3rr(error)( //{...} }

T*e "i a! code may ot %e e#act!y as t*e a%o'e %ut, you see w*at 1 mea , t*e GetT routi e *as to retur t*e error code0 1" it is =, t*ere is o pro%!em, we ca disp!ay temperature, e!se, we disp!ay t*e error code0 Temperature *as to %e set %y t*e routi e some*ow so, you may use poi ters or dec!are it &!o%a!0 Start writi & t*e "i a! GetT routi e6

C%C, Central Remedial Clinic


CRC is a easy %ut power"u! tec* iEue to o%tai "rame data re!ia%i!ity0 You may "i d rea!!y comp!e# e#p!a atio s o *ow it wor,s, t*ey do ot "a!! i to t*e scope o" t*is tutoria!0 Putti & it simp!y, you correspo d a data %yte to a po!y omia! o" t*e "orm FN>LFNCL000LFN=0 3ot* commu icatio sides *a'e a&reed o a di'ider w*ic* is ca!!ed CRC a d i our case is FN(LFNBLFN4LFN=0 T*e tra smitter does t*e di'isio a d appe ds t*e remai der to t*e data "rame0 T*is remai der is ca!!ed c*ec,sum It*is tric, started wit* se di & t*e %it sum i stead o" t*e remai der;0 Now, upo receptio t*e recei'er does t*e di'isio itse!" a d c*ec,s i" t*e remai der is

t*e same as t*e tra smitted o e0 1" it is, commu icatio was "i e0 7*at made t*is tric, popu!ar is t*e ease o" its imp!emetatio , wit* s*i"ts a d #ors we ca &et away0 Read 4a#im-s app!icatio ote D>, *a'e a !oo, at t*e dia&ram a d t*e routi e0

u. ?#?.(u. input u. seed) { u. i feed<ac-( for (i%,( i<.( iJJ) { feed<ac- % ((seed R input) 8 ,E,1)( if (Ffeed<ac-) seed >>% 1( else { seed R% ,E1.( seed >>% 1( seed 5% ,E.,( } input >>% 1( } } return seed(

T*is CRC routi e is "or a si &!e %yte0 Try to &et t*e code "rom t*e dia&ram0 T*e seed is t*e i itia! 'a!ue Ii t*e %o#; o" t*e c*ec,sum t*us, you s*ou!d ca!! t*e routi e wit* seedK=0 T*e e#t step is to create a CRC routi e "or our ( data %ytes0 1t is easier t*a you t*i , it is0
u. ?#?.E.(u. Ninput) { u. i chec-( chec-%,( for (i%,( i<.( iJJ) { chec- % ?#?.(Ninput inputJJ( } } return chec-(

chec-)(

.ac* time t*e ew seed is t*e pre'ious c*ec,sum0 1" you pass a array o" ( %ytes to t*is o e, it s*ou!d retur t*e CRC c*ec,sum w*ic* is t*e Ht* %yte you recei'ed 000 o" course i" commu icatio was "!aw!ess0

Timeout
/,, it !oo,s !i,e our commu icatio c*a proceed wit* t*e rest0 e! is secured to a &ood e#te t0 8et-s

1t is 'ery commo "or microco tro!!ers to po!! e#ter a! de'ices waiti & "or a e'e t to occur0 1 our case we *a'e to wait "or temperature co 'ersio 0 T*e commo practice *ere is to uti!i@e eit*er a timer or a 'aria%!e, w*ic* %y e#piri & wi!! i dicate "ai!ure0 See t*e "o!!owi & e#amp!e a d you wi!! &et t*e idea0
w+E<yte(convert+)( convflag%w#E<yte()( timeout%,( while (Fconvflag 88 timeout<&,,) { "elay(1,,,)( convflag%w#E<yte()( timeoutJJ( } // if (Fconvflag) convert+ timed out // else convert+ is *S // ?onvert +emperature // #ead a <yte ; :nitialiHe timeout // )oll for convertion complete or timeout // #ead a <yte

7e start %y reEuesti & temperature co 'ersio , t*e i stead o" de!ayi & we read a %yte0 As you e#pect, t*is is too "ast "or our t*ermo w*ic* wi!! retur =0 7e i itia!i@e t*e timeout 'aria%!e a d we e ter t*e !oop0 7e wi!! &et out o" t*e !oop w*e o e out o" t*e two co ditio s is met0 .it*er t*e t*ermo wi!! respo d or timeout wi!! occur0 1 t*e !oop, we ,eep po!!i & t*e t*ermo patie t!y %ut, we ad'a ce our timeout 'aria%!e0 A"ter t*e e d o" t*e !oop, we c*ec, w*at we-'e &ot0 P!ease, do t*e same "or t*e s,ipR/4 comma d0 1 am ot sure w*at it retur s or *ow Euic,!y t*e t*ermo respo ds0 3y t*e way, t*i , i" t*e $e!ay is eeded or ot0 1" co 'ersio is comp!eted as e#pected w*at is t*e 'a!ue o" timeout2 Now, re&ardi & t*e Reset si& a!, it is strai&*t "orward0
temp % #eset1Gire()( // #eset =ignal // if (F#eset1Gire()) then no thermo is present // else there is one

Nice, ow you *a'e i creased your code-s re!ia%i!ity, 1 wou!d %e &!ad i" you cou!d rewrite your GetT routi e to retur a error code, = "or o e a d set t*e &!o%a! temperature 'aria%!e0 T*e !atter s*ou!d %e passed %y re"ere ce so use poi ters0 3eware wit* its dec!aratio 0 T*e !ast t*i , you s*ou!d otice is t*at i" a t*ermo is ot prese t, it wi!! e'er do temperature co 'ersio , %ut your error code may %e a!tered0 So, do it i steps a d a"ter eac* step i" t*ere is a error retur 0

Furt er, ideas &or play


1t !oo,s ice, 1 *ope you e joyed t*e tutoria!0 T*e trut* is a project is e'er "i is*ed, it a!ways remai s u der de'e!opme t as we do0 .'e some commercia! products do t*e same0 Oust i case you *a'e -t "i is*ed wit* t*e disp!ay temperature routi e, 1-!! &i'e you some *e!p0 T*e !ast t*i & is to mas, t*e two temperature %ytes to &et t*e si& , t*e i te&er a d decima! parts 000
void "isp+empr (void) { u. decimal%,( u. sign%,( s. temprdisp%,(

decimal % (u.)(tempr 8 ,E,,,4)( sign % (u.)((tempr 8 ,E.,,,)>>1')( temprdisp % (s.)((tempr 8 ,E,14,)>>6)( if (decimal>1) temprdispJJ( if (sign) temprdisp%;temprdisp( } "isplay (temprdisp)(

// mas- decimal part last 6 <its // mas- sign first <it // mas- the integer part 1 <its

/<, w*at e!se2 +mm, you oticed t*at t*e t*ermo ta,es some time to sett!e dow a!so, t*ere are times w*ere it c*a &es "reEue t!y %etwee two adjace t 'a!ues0 7*at 1 su&&est is, &et D4 measureme ts i a array, discard t*e !owest a d t*e *i&*est D a d a'era&e t*e rest D= 'a!ues0 Repeat e'ery time a ew measureme t arri'es, s*i"t out o" t*e %u""er t*e o!dest 'a!ue a d add t*e ew o e i 0 3y a'era&i & Ior !ow:pass "i!teri &; your data wi!! produce a more sta%!e Ior trusted; output0 7*at a%out a ot*er "eature2 Yes, t*e %utto 6 1t wou!d %e ice to *a'e )a*re *eit i stead o" Ce!cius o %utto re!ease, t*e %ac, a&ai to Ce!cius o t*e e#t %utto re!ease0 $o -t "or&et de%ou ci &0 7*at you ca do is c*ec, at t*e %e&i i & o" t*e mai !oop t*e state o" t*e %utto , &et temperature a d t*e c,ec, a&ai t*e state o" t*e %utto 0 Remem%er t*at t*e tota! de!ay s*ou!d %e somet*i & arou d B==msec0 $o some tests to &et t*e timi & ri&*t, our users are ot &oi & to press t*e %utto "or more t*a (==msec0 3y t*e way, *ow !o & *as your mai !oop %ecome2

Bu&&ers
You sti!! wa t t*e temperature !o&&i & uti!ity ri&*t2 /<, !et-s start %y ma,i & our seria! commu icatio more e!e&a t t*us, i terrupt dri'e 0 T*e code t*at wi!! %e prese ted s*ort!y is mai !y t*e code o" t*e AVRG=C app!icatio ote0 ?sua!!y i computer stu"" t*ere is t*e "o!!owi & issue0 Two di""ere t tas,s or processes eed to e#c*a &e data %etwee eac* ot*er "or some reaso 0 Say, t*ere is o e tas, t*at &e erates data a d a ot*er tas, t*at co sumes data0 Not a issue you t*i ,, t*is is e#act!y w*at we do, our GetT routi e &e erates data a d our $isp!ay routi e co sumes data so, just ru t*em i terc*a &ea%!y0 Get data, disp!ay data, &et data, disp!ay data ad i "i itum0 7e!!, t*ese tas,s are sy c*ro ous, wa&o s o" t*e same trai , t*ey mo'e wit* t*e same speed, t*is is w*y it is easy0 7*at i" we *ad a tas, t*at cou!d &e erate data too "ast to %e co sumed2 7e wou!d *a'e to i troduce a ,i d o" memory %etwee t*ese tas,s to so!'e t*e pro%!em0 Yes, a %u""er w*ou!d %e a%!e to *o!d t*e "ast &e erated data "or as !o & as t*e s!ow co sumer eeds t*em0 Now, t*at you *a'e t*e so!utio we may see t*e rea! pro%!em0 7*e a %yte is se t 'ia t*e seria! port, 1= si& a! c*a &es occur at t*e !i e, ( "or t*e ( %its p!us D "or t*e start a d stop i dicators0 7*e commu catio is set at 1HD== %aud rate, it ta,es 1=51HD== sec K *a!" msec "or a %yte to %e commu icated0 7e *a'e a si &!e re&ister a d i a si &!e msec two %ytes wi!! arri'e0 / t*e ot*er *a d, our micro eeds time to ta!, to t*e t*ermometer, ma y de!ays *a'e %ee i troduced, t*e Euestio remai s: +ow ca 1 dea! wit* a!! t*e i comi & %ytes w*e 1 am %usy ta!,i & to t*e t*ermometer2 T*e a swer is simp!e: Store t*em "ast, dea! wit* t*em o your "ree time0 No, t*ere are o micros *a'i & depressio 0

So *ere is *ow it !oo,s !i,e, t*ere is a array t*at *o!ds i comi & %ytes t*at arri'e asy c*ro ous!y wit* our mai !oop a d two poi ters0 T*e 7rite poi ter "or t*e tas, t*at &e erates data, to remem%er w*ere to write to a d t*e Read poi ter "or t*e tas, t*at co sumes data, to remem%er w*ere to read "rom0 3ot* poi ters mo'e ri&*t a d t*ey wrap arou d0

As you wou!d e#pect, t*e 7rite poi ter is a*ead o" t*e Read poi ter a d t*ere are data o !y i %etwee t*ese poi ters0 1" t*e 7rite poi ter is too "ast, it wi!! ma a&e to catc* t*e Read poi ter up a d you wi!! &et a o'er"!ow, t*e e#t %yte t*at wi!! arri'e wi!! "a!! i to t*e s*adows0 1" t*e Read poi ter is too "ast, it wi!! ma a&e to catc* t*e 7rite poi ter up a d you wi!! &et a u der"!ow, ot too %ad, your %u""er wi!! just %e empty0

'nterrupts
+ere "o!!ows a s*ort discriptio o" i terrupts, 1 am sure i" you Goo&!e a %it you wi!! "i d &reat i "o0 So, our micro starts at t*e e try poi t, t*e mai routi e a d e#ecutes comma ds seria!y0 At a poi t i time, a i ter a! or e#ter a! e'e t causes our micro to jump "rom its orma! e#ecutio to a ot*er poi t, t*e 1 terrupt Ser'ice Routi e0 .'ery i terrupt t*at is acti'e *as to *a'e a correspo di & 1SR0 A"ter t*e e d o" t*e 1SR our micro retur s to its orma! e#ecutio 0

Now t*e micro to , ow w*ic* e'e t tri&&ers w*ic* 1SR eeds a ta%!e, t*e 'ector ta%!e0 T*e poi ter t*at i dicates t*e e#t i structio t*at is to %e e#ecuted is ca!!ed Pro&ram Cou ter0 A!! code resides i Pro&ram 4emory I)!as*; w*ic* is w*at t*e "o!!owi & "i&ure prese ts0 / ce a&ai we see t*e Pro&ram Cou ter jumpi & to t*e 'ector ta%!e a d t*e to t*e 1SR0 T*is is rou&*!y t*e story, 1 just *ad a drawi & desire t*is e'e i &, !et-s see t*e rea! t*i &0

1 just copy5paste *ere a piece o" code t*at s*ows t*e use o" t*e e#ter a! i terrupt =0
#include <avr/signal.h> #include <avr/interrupt.h> int main(void) { :nit@A#+ (11)( :nitSey<in ()( sei()( while (forever) { }

// :nitialiHe @A#+ // :nitialiHe Sey<oard // 3na<le :nterrupts

=:>7A2(=:>T:7+3##@)+,) // 3Eternal :nterrupt , { -eyGindow % (-eyGindow<<1) 5 -ey"ata$it( if (-ey>et=etF%&'') -ey>et=et;;( if (-eyGindow%%-ey$rea-) -ey>et=et%1,( if (-ey>et=et%%,) +ransmit$yte(-eyGindow)( }

T*e S1GNA8 ,eyword te!!s t*e compi!er t*at t*is is a 1SR0 T*e S1GP1NT.RR?PT= ,eyword is reser'ed, i dicati & t*at t*is 1SR is "or t*e e#ter a! i terrupt =0 Note t*at t*e 1SR is ot dec!ared a yw*ere e!se i t*e pro&ram0 $o -t "or&et t*e Qi c!udes t*at e#p!ai t*ese t*i &s to t*e compi!er0 Now, i terrupts are e a%!ed wit* t*e sei comma d a d t*e micro e ds up doi & ot*i & i t*e w*i!e !oop, it just jumps to t*e 1SR w*e e'er a e#ter a! i terrupt is tri&&ered0 T*is is a!! you eed0 1" you !oo, i to your project-s .#ter a! $epe de cies, t*e iom(0* "i!e you wi!! "i d t*e a'ai!a%!e i terrupt 'ectors0

T e Button, debounced
+a'e you p!ayed wit* t*e %utto 2 1" yes, you possi%!y rea!i@ed w*at t*e associated pro%!ems are0 7*e to po!! it2 +ow "reEue t!y2 7*at i" it %ou ces up a d dow 2 7e!!, po!!i & ca wor, %ut its a %it o" a pai , *ard to &et t*e timi & ri&*t0

A %etter so!utio w*ou!d %e *a'i & t*e %utto co ected to pi 4 or B w*ere t*e e#etr a! i terrupts are a d dea! wit* it i a 1SR0 3ut, t*e t*i & is, due to %ou ci & you wou!d e d up wit* mu!tip!e i terrupts0 You cou!d cou t you t*i ,0 7e!!, "or *ow !o &2 A de!ay i a 1SR ca ot %e to!erated, 1SRs *a'e to %e Euic,0 3e Euic, or %e dead, as some "rie ds o" mi e su&&est0 Time is t*e ,ey, we cou!d po!! t*e %utto at a co sta t i ter'a! a d c*ec, its state0 T*is i ter'a! s*ou!d %e somet*i & arou d G==:B==ms0 1" !ess, %ou ci & may ot %e o'er, i" more we may miss a c!ic,0 So, we eed a timer a d its o'er"!ow i terrupt0 A (:%it timer is a cou ter, cou ti & "rom = to DBB at t*e micro-s c!oc, "reEue cy0 / e c!oc, period "or a si &!e cou ter i crease0 T*us, t*e i terrupt wi!! *it e'ery DBC5>0G>D(4+@KG40>Dus0 Addi & a 151=D4 timer presca!er wi!! ma,e t*is de!ay G40>DusM1=D4KGB0BBms0 +mm, !oo,s !i,e we *a'e a desce t de!ay t*at wi!! ot de!ay at a!! our code0 Now, si ce t*e de!ay is GBms we ca cou t up to 1= a d t*e read %utto -s state0 No6 T*ere is a possi%i!ity t*at we wi!! po!! t*e %utto duri & t*e %ou ci & period0 T*e %est t*i & to do is, po!! t*e %utto e'ery GBms a d i" "or 1= po!!s its state is t*e same, accept it as 'a!id0 Try to do it0 7*at you eed is strai&*t"orward, i itia!i@e Timer= e a%!i & o'er"!ow i terrupt a d write t*e 1SR0
/// "efines /////////// #define <tn 9):7$ 8 (1<<0) #define ledoff )*#+$ 5% (1<<6) #define ledon )*#+$ 8% 9(1<<6) /// >lo<als /////////// static volatile u. <tn=tate( int main(void) { :nit@A#+(&0)( :nit)orts()( :nit+imer()( sei ()( while (forever) { if (<tn=tate) ledon( else ledoff( } } void :nit+imer(void) { +?7+, % ,( +??#, 5% ,E,'( +:O=S 5% 1<<+*:3,( } =:>7A2(=:>T*U3#42*G,) { static volatile u. old=tate

// #eset // )rescaler 1/1,&6 // 3na<le overflow interrupt // +imer overflow :=# new=tate // // // // count( #ead <utton!s state :f the same advance counter :f not the same reset counter #oll

new=tate % <tn( if (new=tate%%old=tate) countJJ( else count%,( old=tate % new=tate( if (count>1,) { <tn=tate % new=tate( count%,( }

// :f sta<le accept it

.asy to "o!!ow 1 t*i ,, we are just switc*i & o t*e 8.$ o %utto dow 0 Note, t*at our 'aria%!es s*ou!d preser'e t*eir 'a!ues o successi'e 1SR ca!!s0 A!so, 1 t*i , t*is capacitor i para!!e! wit* our %utto did some wor,, 1 wou!d cou t up to 40

Вам также может понравиться