Programming the ATmega16Mechatronics Concept MapRecap Last LectureTest Your ComprehensionStructure of main()ATmega16 Port Pin DetailsPORT Pin SchematicATmega16 Internal ArchitectureATmega16 FeaturesBit ManipulationsSummary of Bit ManipulationPull-up ResistorsRecap ATmega16 Digital I/ORecap ATmega16 Digital I/O, cont.Slide 15Reading PORTA Pins ExampleProgramming the ATmega16B. Furman14SEP2010Mechatronics Concept MapSystem toControlSensorSignalConditioningController(Hardware & Software)PowerInterfaceActuatorUserInterfacePowerSourceBJ Furman 26JAN06ME 106ME 154ME 157ME 195ME 120ME 297AME 106ME 120ME 106ME 190ME 187ME 110ME 136ME 154ME 157ME 182ME 189ME 195ME 106ME 120ME 106Recap Last LectureBinary and hex numbersDigital pins as inputs or outputsPins are bidirectiona l for digital I/ODDRx (x=A, B, C, or D) register determines direction8-bit registera ‘1’ meansa ‘0’ meansmain_RC.cProgrammer’s block#includeInitialization function, init();DDRA, DDRB, and uart_init()01234567Test Your ComprehensionWrite a statement that will make all pins of PORTC to be outputsDDRC =DDRC = 0b11111111;DDRC = 255;Write a statement that will make pins 5, 3, and 1 of PORTC to be outputs, and the rest inputsDDRC = 0b00101010;0xFF;Structure of main()Look again at main_RC.cinit()printf_example()Declaration of variableschar, string, uint8_t, uint16_t, uint32_t, doubleprintf()format stringwhile(1)Why?PORTB = PINA;Explain what is going on as a result of this statementATmega16 Port Pin DetailsSee the ATmega 16 data sheet, p. 50 - 67Port pins consist of three register (special memory location) bits:DDRxData Direction bit in DDRx register (read/write)PORTxnPORTxn bit in PORTx data register (read/write)PINxnPINxn bit in PINx register (read only)PORT Pin SchematicATmega16 Internal Architecturehttp://www.atmel.com/dyn/resources/prod_documents/doc2466.pdfATmega16 Featureshttp://www.atmel.com/dyn/resources/prod_documents/doc2467.pdfBit ManipulationsExamples of how to work with Port pinsSetting bitsClearing bitsToggling bitsMake bits 5 and 3 of PORTB high and the rest lowChallenge:Summary of Bit ManipulationSetting a bit (making it a ‘1’)Bitwise OR the PORTx register with the corresponding bit maskEx. PORTB | = _BV(3);Clearing a bit (making it a ‘0’)Bitwise AND the PORTx register with the corresponding complemented bit maskEx. PORTB & = ~( _BV(3) );Toggling a bit (making it flip)Bitwise XOR the PORTx register with the corresponding bit maskEx. PORTB ^ = _BV(3);Pull-up ResistorsPins configured as INPUTS can be ‘pulled up’ to VTGWhy is this useful?Puts an input pin in a known state (logic high) if no external influence has pulled it down (to logic low)Example of a switch connected between a pin and groundHow is it done?When the pin is configured as an input, SET the corresponding bit in PORTxnUndone by clearing the bitRecap ATmega16 Digital I/OPins are bi-directional. Can configure as:Inputs – _______ determines the pin voltageOutputs – ______ determines the pin voltageDirection determined by bits in DDRx registerWhere x is A – D (and corresponds to all 8 pins associated with the port)If configured as output:Program can specify a pin to be high (VTG) or low (GND) by writing a corresponding 1 or 0 (respectively) to PORTx registerEx. To make Port C pins 7, 3, and 4 low, and the rest highPORTC=___________; (write in binary, then in hex)Recap ATmega16 Digital I/O, cont.If pins configured as input, this means:External device can pull pin voltage high or lowi.e. take up to VTG or take down to GNDYou can determine the state of the portpins by reading the PINx registerGrabs all eight logic levels at the same timeEx. PORTA configured as inputsPA0PA1PA2PA3PA4PA5PA6PA7VTGunsigned char a_pins;a_pins=PINA;What is the content of a_pins:binary:__________hex:_____Recap ATmega16 Digital I/O, cont.If pins configured as input, cont.:Can turn pull-up resistors on or off by writing a 1 or 0 to corresponding pins in PORTxA pull-up resistor internally connects a pin to VTG to give it a defined state (logic high, i.e., 1)Ex. Write the code that will:Make Port A pins i nputsTurn on pull-up resistorsRead the voltages on the pins and store them in a variable, testAWhat is the value of testA in binary and hex?PA0PA1PA2PA3PA4PA5PA6PA7VTGReading PORTA Pins Exampleunsigned char testA;DDRA=0;testA=PINA;What is the content of testA?binary: 11111001hex:
View Full Document