GPIO Driver

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

AVR Microcontrollers – GPIO Driver

by:
Eng. Mohamed Tarek.

© 2022 by Eng. Mohamed Tarek. 1


ATmega16/32 GPIO Driver

▪ GPIO Driver Data Types


1. GPIO_PinDirectionType data type, it is used for the pin direction value.
typedef enum
{
PIN_INPUT,PIN_OUTPUT
}GPIO_PinDirectionType;

2. GPIO_PortDirectionType data type, it is used for port direction value.


typedef enum
{
PORT_INPUT,PORT_OUTPUT=0xFF
}GPIO_PortDirectionType;

© 2022 by Eng. Mohamed Tarek. 2


ATmega16/32 GPIO Driver

Implement GPIO Software Driver


gpio.c and gpio.h
contains:

1. GPIO_setupPinDirection()
2. GPIO_writePin()
3. GPIO_readPin()
4. GPIO_setupPortDirection()
5. GPIO_writePort()
6. GPIO_readPort()

© 2022 by Eng. Mohamed Tarek. 3


ATmega16/32 GPIO Driver

void GPIO_setupPinDirection(uint8 port_num, uint8 pin_num,


GPIO_PinDirectionType direction)
▪ Inputs:
• port_num: ID for the port number.
• pin_num: ID for the pin number.
• direction: The required direction for this pin its value should be PIN_INPUT or
PIN_OUTPUT
▪ Return Value: void
▪ Description:
• Setup the direction of the required pin input/output.
• If the input port number or pin number are not correct, The function will not handle the
request.

© 2022 by Eng. Mohamed Tarek. 4


ATmega16/32 GPIO Driver

void GPIO_writePin(uint8 port_num, uint8 pin_num, uint8 value)


▪ Inputs:
• port_num: ID for the port number.
• pin_num: ID for the pin number.
• value: value to be written on this pin it should be LOGIC_HIGH or LOGIC_LOW
▪ Return Value: void
▪ Description:
• Write the value Logic High or Logic Low on the required pin.
• If the input port number or pin number are not correct, The function will not handle the
request.
• If the pin is input, this function will enable/disable the internal pull-up resistor.

© 2022 by Eng. Mohamed Tarek. 5


ATmega16/32 GPIO Driver

uint8 GPIO_readPin(uint8 port_num, uint8 pin_num)


▪ Inputs:
• port_num: ID for the port number.
• pin_num: ID for the pin number.
▪ Return Value: pin value it should be LOGIC_HIGH or LOGIC_LOW
▪ Description:
• Read and return the value for the required pin, it should be Logic High or Logic Low.
• If the input port number or pin number are not correct, The function will return Logic
Low.

© 2022 by Eng. Mohamed Tarek. 6


ATmega16/32 GPIO Driver

void GPIO_setupPortDirection(uint8 port_num, GPIO_PortDirectionType direction)


▪ Inputs:
• port_num: ID for the port number.
• direction: The required direction for all port its value should be PORT_INPUT or
PORT_OUTPUT
▪ Return Value: void
▪ Description:
• Setup the direction of the required port all pins input/output.
• If the direction value is PORT_INPUT all pins in this port should be input pins.
• If the direction value is PORT_OUTPUT all pins in this port should be output pins.
• If the input port number is not correct, The function will not handle the request.

© 2022 by Eng. Mohamed Tarek. 7


ATmega16/32 GPIO Driver

void GPIO_writePort(uint8 port_num, uint8 value)


▪ Inputs:
• port_num: ID for the port number.
• value: value to be written on all port pins.
▪ Return Value: void
▪ Description:
• Write the value on the required port.
• If any pin in the port is output pin the value will be written.
• If any pin in the port is input pin this will activate/deactivate the internal pull-up
resistor.
• If the input port number is not correct, The function will not handle the request.

© 2022 by Eng. Mohamed Tarek. 8


ATmega16/32 GPIO Driver

uint8 GPIO_readPort(uint8 port_num)


▪ Inputs:
• port_num: ID for the port number.
▪ Return Value: value for all the pins on this port.
▪ Description:
• Read and return the value of the required port.
• If the input port number is not correct, The function will return ZERO value.

© 2022 by Eng. Mohamed Tarek. 9


© 2022 by Eng. Mohamed Tarek. 10
M.T Full Embedded Diploma Contents :
https://www.mediafire.com/file/7ye0w8t3hfs1q44/MT_Embedded_Diploma_Contents_2021.pdf/file

M.T Facebook Group :


https://www.facebook.com/groups/Embedded.Systems.Programming

M.T Linkedin Account :


https://www.linkedin.com/in/mohamed-tarek-2237a457/

Contact Details

Eng. Mohamed Tarek.


Embedded Software Lead.
Mob: 01115154316
mtarek.2013@gmail.com

© 2022 by Eng. Mohamed Tarek. 11

You might also like