Cprogrammingforembeddedmicrocontrollersystems.Assumesexperiencewithassemblylanguageprogramming.V.P.NelsonFall2014-ARMVersionELEC3040/3050EmbeddedSystemsLab(V.P.Nelson)Outline•Programorganizationandmicrocontrollermemory•Datatypes,constants,variables•Microcontrollerregister/portaddresses•Operators:arithmetic,logical,shift•Controlstructures:if,while,for•Functions•InterruptroutinesFall2014-ARMVersionELEC3040/3050EmbeddedSystemsLab(V.P.Nelson)BasicCprogramstructureFall2014-ARMVersionELEC3040/3050EmbeddedSystemsLab(V.P.Nelson)#includeSTM32L1xx.h/*I/Oport/registernames/addressesfortheSTM32L1xxmicrocontrollers*//*Globalvariables–accessiblebyallfunctions*/intcount,bob;//global(static)variables–placedinRAM/*Functiondefinitions*/intfunction1(charx){//parameterxpassedtothefunction,functionreturnsanintegervalueinti,j;//local(automatic)variables–allocatedtostackorregisters--instructionstoimplementthefunction}/*Mainprogram*/voidmain(void){unsignedcharsw1;//local(automatic)variable(stackorregisters)intk;//local(automatic)variable(stackorregisters)/*Initializationsection*/--instructionstoinitializevariables,I/Oports,devices,functionregisters/*Endlessloop*/while(1){//Canalsouse:for(;;){--instructionstoberepeated}/*repeatforever*/}DeclarelocalvariablesInitializevariables/devicesBodyoftheprogramSTM32L100RCµCmemorymapFall2014-ARMVersionELEC3040/3050EmbeddedSystemsLab(V.P.Nelson)0xE00FFFFF0xE00000000x400267FFPeripheralregisters0x400000000x200000000x0803FFFF16KBRAM256KBFlashMemoryCortexregistersControl/dataregisters:Cortex-M3CPUfunctions(NVIC,SysTickTimer,etc.)Control/dataregisters:microcontrollerperipherals(timers,ADCs,UARTs,etc.)256KbyteFlashmemory:programcode&constantdatastorageReset&interruptvectors:in1stwordsofflashmemory0x0800000016KbyteRAM:variable&stackstorageVacantVacantVacantAddress0x20003FFFVacant0xFFFFFFFFVacantMicrocontroller“headerfile”•KeilMDK-ARMprovidesaderivative-specific“headerfile”foreachmicrocontroller,whichdefinesmemoryaddressesandsymboliclabelsforCPUandperipheralfunctionregisteraddresses.#includeSTM32L1xx.h“/*targetuCinformation*///GPIOAconfiguration/dataregisteraddressesaredefinedinSTM32L1xx.hvoidmain(void){uint16_tPAval;//16-bitunsignedvariableGPIOA-MODER&=~(0x00000003);//SetGPIOApinPA0asinputPAval=GPIOA-IDR;//SetPAvalto16-bitsfromGPIOAfor(;;){}/*executeforever*/}Fall2014-ARMVersionELEC3040/3050EmbeddedSystemsLab(V.P.Nelson)Ccompilerdatatypes•Alwaysmatchdatatypetodatacharacteristics!•Variabletypeindicateshowdataisrepresented•#bitsdeterminesrangeofnumericvalues•signed/unsigneddetermineswhicharithmetic/relationaloperatorsaretobeusedbythecompiler•non-numericdatashouldbe“unsigned”•Headerfile“stdint.h”definesalternatetypenamesforstandardCdatatypes•Eliminatesambiguityregarding#bits•Eliminatesambiguityregardingsigned/unsigned(Typesdefinedonnextpage)Fall2014-ARMVersionELEC3040/3050EmbeddedSystemsLab(V.P.Nelson)CcompilerdatatypesDatatypedeclaration*NumberofbitsRangeofvalueschark;unsignedchark;uint8_tk;80..255signedchark;int8_tk;8-128..+127shortk;signedshortk;int16_tk;16-32768..+32767unsignedshortk;uint16_tk;160..65535intk;signedintk;int32_tk;32-2147483648..+2147483647unsignedintk;uint32_tk;320..4294967295*intx_tanduintx_tdefinedinstdint.hFall2014-ARMVersionELEC3040/3050EmbeddedSystemsLab(V.P.Nelson)Datatypeexamples•ReadbitsfromGPIOA(16bits,non-numeric)–uint16_tn;n=GPIOA-IDR;//or:unsignedshortn;•WriteTIM2prescalevalue(16-bitunsigned)–uint16_tt;TIM2-PSC=t;//or:unsignedshortt;•Read32-bitvaluefromADC(unsigned)–uint32_ta;a=ADC;//or:unsignedinta;•Systemcontrolvaluerange[-1000…+1000]–int32_tctrl;ctrl=(x+y)*z;//or:intctrl;•Loopcounterfor100programloops(unsigned)–uint8_tcnt;//or:unsignedcharcnt;–for(cnt=0;cnt20;cnt++){Fall2014-ARMVersionELEC3040/3050EmbeddedSystemsLab(V.P.Nelson)Constant/literalvalues•Decimalisthedefaultnumberformatintm,n;//16-bitsignednumbersm=453;n=-25;•Hexadecimal:prefacevaluewith0xor0Xm=0xF312;n=-0x12E4;•Octal:prefacevaluewithzero(0)m=0453;n=-023;Don’tuseleadingzeroson“decimal”values.Theywillbeinterpretedasoctal.•Character:characterinsinglequotes,orASCIIvaluefollowing“slash”m=‘a’;//ASCIIvalue0x61n=‘\13’;//ASCIIvalue13isthe“return”character•String(array)ofcharacters:unsignedchark[7];strcpy(m,“hello\n”);//k[0]=‘h’,k[1]=‘e’,k[2]=‘l’,k[3]=‘l’,k[4]=‘o’,//k[5]=13or‘\n’(ASCIInewlinecharacter),//k[6]=0or‘\0’(nullcharacter–endofstring)Fall2014-ARMVersionELEC3040/3050EmbeddedSystemsLab(V.P.Nelson)Programvariables•Avariableisanaddressablestoragelocationtoinformationtobeusedbytheprogram–Eachvariablemustbedeclaredtoindicatesizeandtypeofinformationtobestored,plusnametobeusedtoreferencetheinformationintx,y,z;//declares3variablesoftype“int”chara,b;//declares2variablesoftype“char”–Spaceforvariablesmaybeallocatedinregisters,RAM,orROM/Flash(forconstants)–VariablescanbeautomaticorstaticFall2014-ARMVersionELEC3040/3050EmbeddedSystemsLab(V.P.Nelson)Variablearrays•Anarrayisasetofdata,storedinconsecutivememorylocations,beginningatanamedaddress–Declarearraynameandnumberofdataelements,N–Elementsare“indexed”,withindices[0..