DOC PREVIEW
SJSU ME 106 - Programming

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 LectureBinary and hex numbersDigital pins as inputs or outputsPins are bidirectiona l for digital I/ODDRx (x=A, B, C, or D) register determines direction8-bit registera ‘1’ meansa ‘0’ meansmain_RC.cProgrammer’s block#includeInitialization function, init();DDRA, DDRB, and uart_init()01234567Test Your ComprehensionWrite a statement that will make all pins of PORTC to be outputsDDRC =DDRC = 0b11111111;DDRC = 255;Write a statement that will make pins 5, 3, and 1 of PORTC to be outputs, and the rest inputsDDRC = 0b00101010;0xFF;Structure of main()Look again at main_RC.cinit()printf_example()Declaration of variableschar, string, uint8_t, uint16_t, uint32_t, doubleprintf()format stringwhile(1)Why?PORTB = PINA;Explain what is going on as a result of this statementATmega16 Port Pin DetailsSee the ATmega 16 data sheet, p. 50 - 67Port pins consist of three register (special memory location) bits:DDRxData Direction bit in DDRx register (read/write)PORTxnPORTxn bit in PORTx data register (read/write)PINxnPINxn 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 ManipulationsExamples of how to work with Port pinsSetting bitsClearing bitsToggling bitsMake bits 5 and 3 of PORTB high and the rest lowChallenge:Summary of Bit ManipulationSetting a bit (making it a ‘1’)Bitwise OR the PORTx register with the corresponding bit maskEx. PORTB | = _BV(3);Clearing a bit (making it a ‘0’)Bitwise AND the PORTx register with the corresponding complemented bit maskEx. PORTB & = ~( _BV(3) );Toggling a bit (making it flip)Bitwise XOR the PORTx register with the corresponding bit maskEx. PORTB ^ = _BV(3);Pull-up ResistorsPins configured as INPUTS can be ‘pulled up’ to VTGWhy 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 groundHow is it done?When the pin is configured as an input, SET the corresponding bit in PORTxnUndone by clearing the bitRecap ATmega16 Digital I/OPins are bi-directional. Can configure as:Inputs – _______ determines the pin voltageOutputs – ______ determines the pin voltageDirection determined by bits in DDRx registerWhere 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 registerEx. To make Port C pins 7, 3, and 4 low, and the rest highPORTC=___________; (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 lowi.e. take up to VTG or take down to GNDYou can determine the state of the portpins by reading the PINx registerGrabs all eight logic levels at the same timeEx. 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 PORTxA 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 nputsTurn on pull-up resistorsRead the voltages on the pins and store them in a variable, testAWhat 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

SJSU ME 106 - Programming

Download Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?