├── .gitignore ├── GPIO_example.c ├── I2C_LCD_example.c ├── I2C_example.c ├── LCD_example.c ├── LICENSE ├── Makefile ├── README.md ├── SPI_example.c ├── UART_example.c ├── img.jpg ├── img_readme.jpg └── src ├── include ├── config.h ├── gpio.h ├── i2c.h ├── i2c_config.h ├── i2c_lcd.h ├── lcd.h ├── lcd_config.h ├── spi.h ├── spi_config.h ├── uart.h └── uart_config.h └── lib ├── gpio.c ├── i2c.c ├── i2c_lcd.c ├── lcd.c ├── spi.c └── uart.c /.gitignore: -------------------------------------------------------------------------------- 1 | *.hex 2 | -------------------------------------------------------------------------------- /GPIO_example.c: -------------------------------------------------------------------------------- 1 | /** 2 | * AVRLIB: Example source code for using the GPIO API 3 | * 4 | * Copyright (C) 2019, PURANJAY MOHAN. 5 | * This file is part of HD44780 LCD AVR LIBRARY 6 | * 7 | * AVRLIB is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * AVRLIB is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | **/ 20 | 21 | 22 | #include "gpio.h" //Define __MCU__mcuname__ in this file 23 | #include 24 | #include 25 | 26 | int main() 27 | { 28 | gpio_set_mode_output('D',6); 29 | for (;;) 30 | { 31 | gpio_set_high('D',6); //writing a one to PORTD pin 6 32 | _delay_ms(1000); //delay of one second 33 | gpio_set_low('D',6); //writing a zero to PORTD pin 6 34 | _delay_ms(1000); //delay of one second 35 | } 36 | return 0; 37 | } 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /I2C_LCD_example.c: -------------------------------------------------------------------------------- 1 | /** 2 | * AVRLIB: Example source code for using the I2C LCD API 3 | * 4 | * Copyright (C) 2019, PURANJAY MOHAN. 5 | * This file is part of I2C LCD AVR LIBRARY 6 | * 7 | * AVRLIB is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * AVRLIB is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | **/ 20 | 21 | 22 | #include "i2c_lcd.h" 23 | #include 24 | #include 25 | 26 | int main() 27 | { 28 | i2c_lcd_init(DEFADDR); //initialize the LCD with 8 bit mode 29 | i2c_lcd_write_string("HELLO AVR"); //lcd_write_string() prints strings on the LCD 30 | 31 | 32 | i2c_lcd_show_cursor_block(); //lcd_show_cursor_block() turns the cursor to a blinking block 33 | 34 | i2c_lcd_set_cursor(1,0); //lcd_set_cursor() sets the postion of the cursor at (row,column) 35 | 36 | i2c_lcd_write_string("HD44780 LCD API FOR"); 37 | //i2c_lcd_set_cursor(2,0); 38 | //i2c_lcd_write_string("AVR MICROCONTROLLERS"); 39 | //i2c_lcd_set_cursor(3,0); 40 | //i2c_lcd_write_string("BY PURANJAY MOHAN"); 41 | /*for(;;){ 42 | _delay_ms(500); 43 | i2c_lcd_show_cursor_underline(); 44 | _delay_ms(500); 45 | i2c_lcd_show_cursor_block(); 46 | _delay_ms(500); 47 | }*/ 48 | //lcd_clrscr(); //lcd_clrscr() clears the screen 49 | 50 | //lcd_hide_cursor(); //lcd_hide_cursor() hides the cursor from the dislay 51 | //_delay_ms(1000); 52 | //lcd_show_cursor_underline(); //lcd_show_cursor_underline() shows the cursor as an underscore 53 | return 0; 54 | 55 | } 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /I2C_example.c: -------------------------------------------------------------------------------- 1 | /** 2 | * AVRLIB: Example source code for using the I2C API 3 | * 4 | * Copyright (C) 2019, PURANJAY MOHAN. 5 | * This file is part of I2C AVR LIBRARY 6 | * 7 | * AVRLIB is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * AVRLIB is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | **/ 20 | #include "gpio.h" 21 | #include "i2c.h" 22 | #include 23 | #include 24 | 25 | 26 | int main() 27 | { 28 | gpio_set_mode_output('D',6); 29 | 30 | unsigned int address=0x7E, i=0; 31 | i2c_init(); 32 | i2c_start(address); 33 | for(i=0; i<5; i++){ 34 | i2c_write(0x00); 35 | _delay_ms(1000); 36 | i2c_write(0xFF); 37 | _delay_ms(1000); 38 | } 39 | i2c_stop(); 40 | return 0; 41 | } 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /LCD_example.c: -------------------------------------------------------------------------------- 1 | /** 2 | * AVRLIB: Example source code for using the LCD API 3 | * 4 | * Copyright (C) 2019, PURANJAY MOHAN. 5 | * This file is part of HD44780 LCD AVR LIBRARY 6 | * 7 | * AVRLIB is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * AVRLIB is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | **/ 20 | 21 | 22 | #include "lcd.h" 23 | #include 24 | #include 25 | 26 | int main() 27 | { 28 | lcd_init(1); //initialize the LCD with 8 bit mode 29 | lcd_write_string("HELLO AVR"); //lcd_write_string() prints strings on the LCD 30 | 31 | 32 | lcd_show_cursor_block(); //lcd_show_cursor_block() turns the cursor to a blinking block 33 | 34 | lcd_set_cursor(1,0); //lcd_set_cursor() sets the postion of the cursor at (row,column) 35 | 36 | lcd_write_string("HD44780 LCD API FOR"); 37 | lcd_set_cursor(2,0); 38 | lcd_write_string("AVR MICROCONTROLLERS"); 39 | lcd_set_cursor(3,0); 40 | lcd_write_string("BY PURANJAY MOHAN"); 41 | for(;;){ 42 | _delay_ms(500); 43 | lcd_show_cursor_underline(); 44 | _delay_ms(500); 45 | lcd_show_cursor_block(); 46 | _delay_ms(500); 47 | } 48 | //lcd_clrscr(); //lcd_clrscr() clears the screen 49 | 50 | //lcd_hide_cursor(); //lcd_hide_cursor() hides the cursor from the dislay 51 | //_delay_ms(1000); 52 | //lcd_show_cursor_underline(); //lcd_show_cursor_underline() shows the cursor as an underscore 53 | return 0; 54 | } 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ####Makefile for LCD API#### 2 | ### 3 | # HD44780 LCD AVR LIBRARY: API related functions 4 | # 5 | # Copyright (C) 2019, PURANJAY MOHAN. 6 | # This file is part of HD44780 LCD AVR LIBRARY 7 | # 8 | # HD44780 LCD AVR LIBRARY is free software: you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation, either version 3 of the License, or 11 | # (at your option) any later version. 12 | # 13 | # HD44780 LCD AVR LIBRARY is distributed in the hope that it will be useful, 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | # GNU General Public License for more details. 17 | # 18 | # You should have received a copy of the GNU General Public License 19 | # along with this program. If not, see . 20 | ### 21 | ### 22 | CC=avr-gcc 23 | OBJCOPY=avr-objcopy 24 | INC=-Isrc/include 25 | LIB=src/lib/ 26 | ### 27 | 28 | #Define the AVR Microcontroller here 29 | MCU=atmega32 30 | #Define the name of your C file here 31 | TARGET=SPI_example 32 | #add all the APIs you need using $(LIB)api1 $(LIB)api2 ... format. 33 | OBJ=$(TARGET).o $(LIB)lcd.o $(LIB)gpio.o $(LIB)uart.o $(LIB)i2c.o $(LIB)i2c_lcd.o $(LIB)spi.o 34 | 35 | 36 | CFLAGS= -mmcu=$(MCU) -Os -Wall $(INC) 37 | 38 | DEPS=lcd.h 39 | 40 | REMOVE=rm -f 41 | 42 | PROG=usbasp 43 | 44 | all: $(TARGET).hex 45 | $(REMOVE) $(TARGET).o $(TARGET).elf $(OBJ) 46 | echo "Done Comiling and Linking" 47 | 48 | $(TARGET).hex: $(TARGET).elf 49 | $(OBJCOPY) -O ihex -j .text -j .data $(TARGET).elf $(TARGET).hex 50 | 51 | $(TARGET).elf: $(OBJ) 52 | $(CC) $(CFLAGS) $(OBJ) --output $(TARGET).elf 53 | 54 | %.o: %.c $(DEPS) 55 | $(CC) $(CFLAGS) -c -o $@ $< 56 | 57 | src/lib/%.o: src/lib/%.c 58 | $(CC) $(CFLAGS) -c -o $@ $< 59 | 60 | clean: 61 | $(REMOVE) $(TARGET).hex $(OBJ) 62 | 63 | program: all 64 | avrdude -c $(PROG) -p $(MCU) -U flash:w:$(TARGET).hex 65 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # AVRLIB 3 | Hardware Abstraction Library for AVR Microcontrollers. 4 | 5 | AVRLIB is a high level Hardware Abstarction API library for interfacing LCDs, GPIO, USART etc. to the AVR series of Microcontrollers. 6 | ![alt text](https://github.com/puranjaymohan/avrlib/blob/master/img_readme.jpg?raw=true) 7 | ## Table of Contents 8 | 9 | [Features](#features) 10 | 11 | [How to Program](#program) 12 | 13 | [Makefile](#makefile) 14 | 15 | [How to use the API in your project](#howtouse) 16 | 17 | [LCD API](#lcdapi) 18 | 19 | [GPIO API](#gpioapi) 20 | 21 | [UART API](#uartapi) 22 | 23 | [I2C API](#i2capi) 24 | 25 | [License](#license) 26 | 27 | 28 | 29 | ## Features of this library: 30 | 31 | - Written in C, well documented and includes many examples. 32 | - Includes APIs for most peripherals like LCD, RTC, GPIO, UART, I2C etc. 33 | - Most of the funtions required by a peripheral have predefined APIs functions. 34 | - Supports the complete family of AVR Microcontrollers. 35 | 36 | > Every API Provides a set of funtions to interface the peripheral. 37 | > Every funtions is documented in the respective API's header file file. 38 | > Every API has a API_example.c file which shows the usage of the funtions. 39 | 40 | 41 | 42 | ## Programming the Microcontroller 43 | This library comes with a Makefile which includes configurations and targets for programming the controller using AVRDUDE. The microcontroller can be programmed via any other method if required. 44 | 45 | 46 | 47 | ## Makefile 48 | The Makefile has to be configured for using it properly. 49 | all APIs that you wish to use need to be added to the makefile variable OBJ=$(LIB)"API NAME" without quotes. 50 | example: 51 | ```Makefile 52 | #Define the AVR Microcontroller here 53 | MCU=atmega32 54 | #Define the name of your C file here 55 | TARGET=example 56 | #add all the APIs you need using $(LIB)api1 $(LIB)api2 ... format. 57 | OBJ=$(TARGET).o $(LIB)lcd.o 58 | ``` 59 | 60 | 61 | 62 | ## Using an API in your project 63 | 1. define the microcontroller you are using in the Makefile using the MCU variable. 64 | ```Makefile 65 | #Define the AVR Microcontroller here 66 | MCU=atmega32 67 | ``` 68 | 2. include the API in your project by adding #include "API.h" to the top of your source file. 69 | ```c 70 | #include "gpio.h" 71 | ``` 72 | 73 | 3. Read the API.h file for configuration and function documentation and use the funtions in your project. 74 | 75 | 4. Add all names of all the APIs that you have used in your project in the Makefile in the OBJ variable using the following syntax 76 | ```Makefile 77 | #add all the APIs you need using $(LIB)api1 $(LIB)api2 ... format. 78 | #Don't remove the $(TARGET).o , just add all the required apis after $(TARGET).o. Examole for using LCD and GPIO api 79 | OBJ=$(TARGET).o $(LIB)lcd.o $(LIB)gpio.o 80 | ``` 81 | 5. Edit the config.h file present in src/include/ and add details about the cpu frequency and target microcontroller 82 | ```Makefile 83 | #define __MCU__ATMEGA32__ 84 | #define F_CPU 16000000UL 85 | ``` 86 | 87 | 88 | 89 | # LCD API 90 | This API provides functions to interface HD44780 LCD in both 4 and 8 bit mode. This only supports 16x2 and 20x4 LCDs, but support for other LCDs can be added easily. 91 | ## Configuring the API 92 | Edit the lcd_config.h header file which can be found at src/include/, in this file define the pins which you have used to connect you LCD to your microcontroller. 93 | Change the file according to your connections, variables which need changes are given below. 94 | ```c 95 | /*LCD TYPES 96 | * define the lcd type with the options given below 97 | * 1 - 16 X 2 98 | * 2 - 20 X 4 */ 99 | #define LCDTYPE 1 100 | 101 | /*PIN CONFIGURATIONS 102 | *define the pins conncted to the lcd 103 | *DATAPORT is the port connected to data pins 104 | *similarly define all connections*/ 105 | #define DATAPORT PORTC 106 | #define DATAPIN PINC 107 | #define DATADDR DDRC 108 | #define RS PD3 109 | #define RW PD4 110 | #define EN PD7 111 | #define CONPORT PORTD 112 | #define CONPIN PIND 113 | #define CONDDR DDRD 114 | ``` 115 | 116 | ## Functions Provided by LCD API 117 | ### 1. lcd_init() 118 | This functions initilizes the LCD and sets up all the registers. It has to be called before using any other lcd api function. 119 | It takes mode as the parameter and doesn't return anything. 120 | mode = 0 --> 8 bit mode. 121 | mode = 1 --. 4 bit mode. 122 | 123 | Parameters - __unsigned int__ mode 124 | 125 | Returns - __Void__ 126 | 127 | ### 2. lcd_send_command(__unsigned char__ command) 128 | This function can be called to send a command to the lcd in 8 bit format, for example 0x01 (clear screen). 129 | 130 | Parameters - __unsigned char__ command 131 | 132 | Returns - __Void__ 133 | 134 | __Example:__ 135 | ```c 136 | unsigned char cmd = 0x01; 137 | lcd_send_command(cmd); 138 | ``` 139 | ### 3. lcd_send_data(__unsigned char__ data) 140 | This function can be called to send data(single character) to the lcd in, for example 'P'. 141 | 142 | Parameters - __unsigned char__ data 143 | 144 | Returns - __Void__ 145 | 146 | __Example:__ 147 | ```c 148 | unsigned char data = 'P'; 149 | lcd_send_command(data); 150 | ``` 151 | ### 4. lcd_write_string(__char *__ string) 152 | This function can be called to send a string to the lcd, for example "HELLO LCD". 153 | It takes a character pointer to a string and return nothing. 154 | 155 | Parameters - __char *__ command 156 | 157 | Returns - __Void__ 158 | 159 | __Example:__ 160 | ```c 161 | char *string = "HELLO LCD :D"; 162 | lcd_send_command(string); 163 | ``` 164 | ### 5. lcd_set_cursor(__int__ row, __int__ column) 165 | This function can be called to change the position of the cursor to any row and column of the LCD, for example 2,0 (second row, first column). It takes two integers as parameters for row and column, it assumes 0,0 as first row and first column. 166 | 167 | Parameters - __int__ row, __int__ column 168 | 169 | Returns - __Void__ 170 | 171 | __Example:__ 172 | ```c 173 | int row = 2; 174 | int col = 1; 175 | lcd_set_cursor(row,col); 176 | ``` 177 | ### 6. lcd_clrscr() 178 | This function can be called to clear the screen and bring the cursor to home position (0,0). 179 | 180 | Parameters - __void__ 181 | 182 | Returns - __Void__ 183 | 184 | __Example:__ 185 | ```c 186 | lcd_clrscr(); 187 | ``` 188 | ### 7. lcd_clrscr() 189 | This function can be called to clear the screen and bring the cursor to home position (0,0). 190 | 191 | Parameters - __void__ 192 | 193 | Returns - __Void__ 194 | 195 | __Example:__ 196 | ```c 197 | lcd_clrscr(); 198 | ``` 199 | ### 8. lcd_show_cursor_underline() 200 | This function can be called to make the cursor appear as an underscore i.e. '_'. 201 | 202 | Parameters - __void__ 203 | 204 | Returns - __Void__ 205 | 206 | __Example:__ 207 | ```c 208 | lcd_show_cursor_underline(); 209 | ``` 210 | ### 9. lcd_show_cursor_block() 211 | This function can be called to make the cursor appear as a block or a box. 212 | 213 | Parameters - __void__ 214 | 215 | Returns - __Void__ 216 | 217 | __Example:__ 218 | ```c 219 | lcd_show_cursor_block(); 220 | ``` 221 | ### 10. lcd_hide_cursor() 222 | This function can be called to hide the cursor, any of the above two functions can be called to get back the cursor. 223 | 224 | Parameters - __void__ 225 | 226 | Returns - __Void__ 227 | 228 | __Example:__ 229 | ```c 230 | lcd_hide_cursor(); 231 | ``` 232 | 233 | 234 | 235 | # GPIO API 236 | This API provides functions to interface GPIOs. It provides functions for easy usage of gpios, the functions are similar to arduino's pinMode and digitalWrite functions. 237 | ## Configuring the API 238 | Edit the config.h header file which can be found at src/include/, in this file define the crystal frequency and the mcu name. This API doesn't requires any other configurations 239 | ## Functions Provided by GPIO API 240 | ### 1. gpio_set_mode_output(__char__ port, __unsigned int__ gpio) 241 | This function initializes the given gpio of the given port as an output. 242 | It takes Port name and pin as parameters, doesn't return anything. 243 | 244 | Parameters - __char__ port, __unsigned int__ gpio 245 | 246 | Returns - __Void__ 247 | 248 | __Example:__ 249 | ```c 250 | gpio_set_mode_output('B',5); // Initializing pin 5 of port B as an output 251 | ``` 252 | ### 2. gpio_set_mode_input(__char__ port, __unsigned int__ gpio, __unsigned int__ pull); 253 | This function sets the gpio of given port as an input. 254 | It takes Port name, pin, and pull(0 or 1) as parameters, doesn't return anything. 255 | 256 | Parameters - __char__ port, __unsigned int__ gpio, __unsigned int__ pull 257 | 258 | Returns - __Void__ 259 | 260 | __Example:__ 261 | ```c 262 | gpio_set_mode_input('B',5,1); // Initializing pin 5 of port B as an input with internal pull-up 263 | gpio_set_mode_input('B',0,0); // Initializing pin 0 of port B as an input with internal pull-down 264 | ``` 265 | ### 3. gpio_set_low(__char__ port, __unsigned int__ gpio); 266 | This function writes a 0 to the given pin of the given port. 267 | 268 | Parameters - __char__ port, __unsigned int__ gpio 269 | 270 | Returns - __Void__ 271 | 272 | __Example:__ 273 | ```c 274 | gpio_set_low('B',3); //Writing a 0 to pin 3 of Port B 275 | ``` 276 | ### 4. gpio_set_high(__char__ port, __unsigned int__ gpio); 277 | This function writes a 1 to the given pin of the given port. 278 | 279 | Parameters - __char__ port, __unsigned int__ gpio 280 | 281 | Returns - __Void__ 282 | 283 | __Example:__ 284 | ```c 285 | gpio_set_high('A',3); //Writing a 1 to pin 3 of Port B 286 | ``` 287 | ### 5. unsigned int gpio_read_pin(__char__ port, __unsigned int__ gpio); 288 | This function reads the pin of given port and returns the value as an int(0 or 1) 289 | 290 | Parameters - __char__ port, __unsigned int__ gpio 291 | 292 | Returns - __unsigned int__ value 293 | 294 | __Example:__ 295 | ```c 296 | unsigned int value; 297 | value = gpio_read_pin("C", 3); //reading the value of pin 3 of Port C and stroing it in value. 298 | ``` 299 | 300 | 301 | 302 | # UART API 303 | This API provides functions to interface the UART peripheral of the microcontroller. 304 | ## Configuring the API 305 | Edit the uart_config.h header file which can be found at src/include/, in this file define the baud rate for the uart communication. This API doesn't requires any other configurations 306 | ## Functions Provided by UART API 307 | ### 1. uart_init(__void__) 308 | This Functions initializes the uart register with the given baud rate and data frame specification, they can exclusively be edited in src/lib/uart.c 309 | This function has to be called before using any other API functions 310 | 311 | Parameters - __Void__ 312 | 313 | Returns - __Void__ 314 | 315 | __Example:__ 316 | ```c 317 | uart_init(); // Initializing uart with baud rate given in uart_config.h 318 | ``` 319 | ### 2. char uart_receive(__void__); 320 | This function reads the uart buffer and returns the data as an unsigned character. 321 | 322 | Parameters - __Void__ 323 | 324 | Returns - __char__ 325 | 326 | __Example:__ 327 | ```c 328 | char data; 329 | data = uart_receive(); 330 | ``` 331 | ### 3. uart_send(__char__ data); 332 | This function can be called to send data through UART as an unsigned character(one byte). 333 | 334 | Parameters - __char__ data 335 | 336 | Returns - __Void__ 337 | 338 | __Example:__ 339 | ```c 340 | uart_send('P'); 341 | ``` 342 | ### 4. uart_send_string(__char*__ StringPtr); 343 | This function can be called to send a string through the UART by providing the pointer to the string as the parameter. 344 | 345 | Parameters - __char*__ ptr 346 | 347 | Returns - __Void__ 348 | 349 | __Example:__ 350 | ```c 351 | char string[] = "Hello"; 352 | uart_send_string(string); 353 | ``` 354 | ### 5. uart_receive_string(__char*__ StringPtr, __unsigned int__ len); 355 | This function can be called to receive a string through the UART by providing the pointer to the place where the string has to be stored and also the length of the string. 356 | 357 | Parameters - __char*__ ptr 358 | 359 | Returns - __Void__ 360 | 361 | __Example:__ 362 | ```c 363 | unsigned int len = 5; 364 | char string[5]; 365 | uart_receive_string(string, len); 366 | ``` 367 | 368 | 369 | 370 | # I2C API 371 | This API provides functions to interface the I2C peripheral of the microcontroller in master mode. 372 | ## Configuring the API 373 | Edit the i2c_config.h header file which can be found at src/include/, in this file define the scl frequency for the i2c communication. This API doesn't requires any other configurations 374 | ## Functions Provided by I2C API 375 | ### 1. i2c_init(__void__) 376 | This Functions initializes the i2c TWBR register with the given frequency and specifications, they can exclusively be edited in src/lib/i2c.c 377 | This function has to be called before using any other API functions 378 | 379 | Parameters - __Void__ 380 | 381 | Returns - __Void__ 382 | 383 | __Example:__ 384 | ```c 385 | i2c_init(); // Initializing i2c with frequency given in i2c_config.h 386 | ``` 387 | ### 2. __unsigned int__ i2c_start(__unsigned int__ address); 388 | This function creates the start condition on the i2c bus and intitializes the communication with the device with given address, which is in the form 7bit address + R/W. 389 | 390 | Parameters - __unsigned int__ address 391 | It is the slave address of the slave device. 392 | Returns - __unsigned int__ error 393 | It returns the status for the api call:- 394 | 3 :- Start condition couldn't be created on the bus. 395 | 0 :- Successfully created start, SLA+W/R condition and ACK recieved. 396 | 1 :- Successfully created start, SLA+W/L condition and NACK recieved. 397 | 2 :- Successfully created start condition, but error in SLA+R/W. 398 | 399 | __Example:__ 400 | ```c 401 | unsigned int error; 402 | error = i2c_start(); 403 | if (!error){ 404 | /*user code*/ 405 | } 406 | ``` 407 | ### 3. __unsigned int__ i2c_repeated_start(__unsigned int__ address); 408 | This function creates the repeated start condition on the i2c bus and intitializes the communication with the device with given address, which is in the form 7bit address + R/W. 409 | 410 | Parameters - __unsigned int__ address 411 | It is the slave address of the slave device. 412 | Returns - __unsigned int__ error 413 | It returns the status for the api call:- 414 | 3 :- Repeated Start condition couldn't be created on the bus. 415 | 0 :- Successfully created repeated start, SLA+W/R condition and ACK recieved. 416 | 1 :- Successfully created repeated start, SLA+W/L condition and NACK recieved. 417 | 2 :- Successfully created repeated start condition, but error in SLA+R/W. 418 | 419 | __Example:__ 420 | ```c 421 | unsigned int error; 422 | error = i2c_repeated_start(); 423 | if (!error){ 424 | /*user code*/ 425 | } 426 | ``` 427 | ### 4. __unsigned int__ i2c_write(__char__ data); 428 | This function sends the 8 bit data on the bus. 429 | 430 | Parameters - __char__ data 431 | It is data to be transmitted on the bus. 432 | Returns - __unsigned int__ error 433 | It returns the status for the api call:- 434 | 0 :- Successfully sent the data and ACK recieved. 435 | 1 :- Successfully sent the data and NACK recieved. 436 | 2 :- An error occured in transmission. 437 | 438 | __Example:__ 439 | ```c 440 | unsigned int error; 441 | char data = 0xff; 442 | error = i2c_send(data); 443 | switch (error){ 444 | /*user code*/ 445 | } 446 | ``` 447 | 448 | ### 5. __char data__ i2c_read(__unsigned int__ send_nack); 449 | This function sends the 8 bit data on the bus. 450 | 451 | Parameters - __unsigned int__ send_nack 452 | If it is equal to 0 then ACK is sent after receiving the data, else NACK is sent. 453 | Returns - __char__ data 454 | It returns the data read from the bus. 455 | __Example:__ 456 | ```c 457 | char data; 458 | data = i2c_read(0); //for ACK after receiving 459 | data = i2c_read(1); //for NACK after receiving 460 | ``` 461 | 462 | ### 6. __Void__ i2c_stop(__void__); 463 | This stops the i2c communication. 464 | 465 | Parameters - __void__ 466 | Returns - __void__ 467 | __Example:__ 468 | ```c 469 | i2c_stop(); 470 | ``` 471 | 472 | 473 | 474 | ### LICENCE 475 | #### GNU General Public License Version 3 476 | -------------------------------------------------------------------------------- /SPI_example.c: -------------------------------------------------------------------------------- 1 | /** 2 | * AVRLIB: Example source code for using the SPI API 3 | * 4 | * Copyright (C) 2019, PURANJAY MOHAN. 5 | * This file is part of SPI AVR LIBRARY 6 | * 7 | * AVRLIB is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * AVRLIB is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | **/ 20 | 21 | #include "spi.h" 22 | #include "i2c_lcd.h" 23 | #include 24 | #include 25 | 26 | int main() 27 | { 28 | unsigned int data = 0x51, datar; 29 | spi_init(); 30 | i2c_lcd_init(DEFADDR); 31 | while(1){ 32 | datar = spi_transmit_recieve(data); 33 | i2c_lcd_send_data((char)datar); 34 | } 35 | return 0; 36 | } -------------------------------------------------------------------------------- /UART_example.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "uart.h" 3 | #include 4 | #include 5 | 6 | unsigned int i; 7 | char c, *d; 8 | unsigned int len = 5; 9 | int main(void){ 10 | uart_init(); 11 | d=(char *)malloc(sizeof(char)*len); 12 | uart_send_string("hello UART\n\r"); 13 | _delay_ms(10); 14 | uart_send_string("please send a character\n\r"); 15 | c=uart_receive(); 16 | uart_send_string("Recieved Char : "); 17 | uart_send(c); 18 | uart_send_string("\n\r"); 19 | 20 | uart_send_string("please enter a string\n\r"); 21 | uart_receive_string(d,len); 22 | uart_send_string(d); 23 | uart_send_string("\n\r"); 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /img.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/puranjaymohan/AVRLIB/ff7172580e3235e3e5db93f776257c3e65db6557/img.jpg -------------------------------------------------------------------------------- /img_readme.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/puranjaymohan/AVRLIB/ff7172580e3235e3e5db93f776257c3e65db6557/img_readme.jpg -------------------------------------------------------------------------------- /src/include/config.h: -------------------------------------------------------------------------------- 1 | #ifndef CONFIG_H 2 | #define CONFIG_H 3 | 4 | #define __MCU__ATMEGA32__ 5 | #define F_CPU 16000000UL 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /src/include/gpio.h: -------------------------------------------------------------------------------- 1 | /** 2 | * AVRLIB: Header file for GPIO API configurations and function prototypes 3 | * 4 | * Copyright (C) 2019, PURANJAY MOHAN. 5 | * This file is part of AVRLIB 6 | * 7 | * AVRLIB is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * AVRLIB is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | **/ 20 | 21 | #ifndef GPIO_H 22 | #define GPIO_H 23 | #include "config.h" 24 | 25 | 26 | /*Functions provided by GPIO API*/ 27 | 28 | /*This function sets the gpio of given port as an output*/ 29 | void gpio_set_mode_output(char port, unsigned int gpio); 30 | 31 | /*This function sets the gpio of given port as an input*/ 32 | void gpio_set_mode_input(char port, unsigned int gpio, unsigned int pull); 33 | 34 | /*This function writes a 0 to the given pin of the given port */ 35 | void gpio_set_low(char port, unsigned int gpio); 36 | 37 | /*This function writes a 1 to the given pin of the given port*/ 38 | void gpio_set_high(char port, unsigned int gpio); 39 | 40 | /*This function reads the pin of given port and returns the value as an int(0 or 1)*/ 41 | unsigned int gpio_read_pin(char port, unsigned int gpio); 42 | #endif 43 | -------------------------------------------------------------------------------- /src/include/i2c.h: -------------------------------------------------------------------------------- 1 | /** 2 | * AVRLIB: Header file for I2C API configurations and function prototypes 3 | * 4 | * Copyright (C) 2019, PURANJAY MOHAN. 5 | * This file is part of AVRLIB 6 | * 7 | * AVRLIB is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * AVRLIB is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | **/ 20 | 21 | #ifndef _I2C_H_ 22 | #define _I2C_H_ 23 | #include "config.h" 24 | #include "i2c_config.h" 25 | #include 26 | //TWBR Calculation based on SCL_CLK 27 | #define BITRATE(TWSR) ((F_CPU/SCL_CLK)-16)/(2*pow(4,(TWSR&((1<. 19 | **/ 20 | 21 | #ifndef I2C_LCD_H 22 | #define I2C_LCD_H 23 | #include "config.h" 24 | /*Data Word Definition for PCF8574*/ 25 | /*DB7 DB6 DB5 DB4 P3 EN RW RS*/ 26 | /*P7 P6 P5 P4 P3 P2 P1 P0*/ 27 | 28 | #define RS (1<<0) 29 | #define RW (1<<1) 30 | #define EN (1<<2) 31 | #define geti2c(data) (data << 4) 32 | 33 | /*DEFAULT ADDRESS*/ 34 | #define DEFADDR 0x40 35 | /*COMMAND DEFINITIONS*/ 36 | 37 | #define CLRSCR 0x01 38 | #define BIT8LINE2 0x38 39 | #define CURSORVISIBLE 0x0E 40 | #define GOTOHOME 0x80 41 | 42 | /*FUNCTIONS PROVIDED BY THIS API*/ 43 | 44 | /*This function has to be called before using any other lcd functions. 45 | * It initializes the LCD in 4bit or 8bit modes. 46 | * mode = 0 --> 8 bit mode 47 | * mode = 1 --> 4 bit mode*/ 48 | void i2c_lcd_init(unsigned int addr); 49 | 50 | /*This function sends data to the lcd and displays it, it takes a character as a parameter*/ 51 | void i2c_lcd_send_data(unsigned char data); 52 | 53 | /*This funtion sends commands to the lcd and takes a character as a parameter*/ 54 | void i2c_lcd_send_command(unsigned char cmd); 55 | 56 | /*This funtion can be used to print strings on the LCD and it takes character pointer as a parameter*/ 57 | void i2c_lcd_write_string(char *string); 58 | 59 | /*This funtion is used to set the position of the cursor on the LCD display 60 | * it takes two parameter which define the row and column of the final position of the cursor 61 | * so second row and fifth column is (1,4) because this functions counts from 0 62 | * home position is (0,0)*/ 63 | void i2c_lcd_set_cursor(int row, int column); 64 | 65 | /*This funtion is used to clear the screen and erase everything written on the LCD*/ 66 | void i2c_lcd_clrscr(void); 67 | 68 | /*This funtion shows the underline (underscore) cursor if the cursor is hidden*/ 69 | void i2c_lcd_show_cursor_underline(void); 70 | 71 | /*This futions turns on the block blinking cursor*/ 72 | void i2c_lcd_show_cursor_block(void); 73 | 74 | /*This funtion hides the cursor and only text remains on the LCD. 75 | * cursor can be brought back by calling lcd_show_cursor() function*/ 76 | void i2c_lcd_hide_cursor(void); 77 | #endif 78 | -------------------------------------------------------------------------------- /src/include/lcd.h: -------------------------------------------------------------------------------- 1 | /** 2 | * AVRLIB: Header file for LCD API configurations and function prototypes 3 | * 4 | * Copyright (C) 2019, PURANJAY MOHAN. 5 | * This file is part of HD44780 LCD AVR LIBRARY 6 | * 7 | * AVRLIB is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * AVRLIB is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | **/ 20 | 21 | #ifndef LCD_H 22 | #define LCD_H 23 | #include "config.h" 24 | #include "lcd_config.h" 25 | 26 | /*COMMAND DEFINITIONS*/ 27 | 28 | #define CLRSCR 0x01 29 | #define BIT8LINE2 0x38 30 | #define CURSORVISIBLE 0x0E 31 | #define GOTOHOME 0x80 32 | 33 | /*FUNCTIONS PROVIDED BY THIS API*/ 34 | 35 | /*This function has to be called before using any other lcd functions. 36 | * It initializes the LCD in 4bit or 8bit modes. 37 | * mode = 0 --> 8 bit mode 38 | * mode = 1 --> 4 bit mode*/ 39 | void lcd_init(unsigned int mode); 40 | 41 | /*This function sends data to the lcd and displays it, it takes a character as a parameter*/ 42 | void lcd_send_data(unsigned char data); 43 | 44 | /*This funtion sends commands to the lcd and takes a character as a parameter*/ 45 | void lcd_send_command(unsigned char cmd); 46 | 47 | /*This funtion can be used to print strings on the LCD and it takes character pointer as a parameter*/ 48 | void lcd_write_string(char *string); 49 | 50 | /*This funtion is used to set the position of the cursor on the LCD display 51 | * it takes two parameter which define the row and column of the final position of the cursor 52 | * so second row and fifth column is (1,4) because this functions counts from 0 53 | * home position is (0,0)*/ 54 | void lcd_set_cursor(int row, int column); 55 | 56 | /*This funtion is used to clear the screen and erase everything written on the LCD*/ 57 | void lcd_clrscr(void); 58 | 59 | /*This funtion shows the underline (underscore) cursor if the cursor is hidden*/ 60 | void lcd_show_cursor_underline(void); 61 | 62 | /*This futions turns on the block blinking cursor*/ 63 | void lcd_show_cursor_block(void); 64 | 65 | /*This funtion hides the cursor and only text remains on the LCD. 66 | * cursor can be brought back by calling lcd_show_cursor() function*/ 67 | void lcd_hide_cursor(void); 68 | #endif 69 | -------------------------------------------------------------------------------- /src/include/lcd_config.h: -------------------------------------------------------------------------------- 1 | #ifndef _LCD_CONFIG_H 2 | #define _LCD_CONFIG_H 3 | 4 | /*LCD TYPES 5 | * define the lcd type with the options given below 6 | * 1 - 16 X 2 7 | * 2 - 20 X 4 */ 8 | #define LCDTYPE 2 9 | 10 | /*PIN CONFIGURATIONS 11 | *define the pins conncted to the lcd 12 | *DATAPORT is the port connected to data pins 13 | *similarly define all connections*/ 14 | #define DATAPORT PORTC 15 | #define DATAPIN PINC 16 | #define DATADDR DDRC 17 | #define RS PD3 18 | #define RW PD4 19 | #define EN PD7 20 | #define CONPORT PORTD 21 | #define CONPIN PIND 22 | #define CONDDR DDRD 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /src/include/spi.h: -------------------------------------------------------------------------------- 1 | /** 2 | * AVRLIB: Header file for SPI API configurations and function prototypes 3 | * 4 | * Copyright (C) 2019, PURANJAY MOHAN. 5 | * This file is part of AVRLIB 6 | * 7 | * AVRLIB is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * AVRLIB is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | **/ 20 | 21 | #ifndef _SPI_H_ 22 | #define _SPI_H_ 23 | #include "config.h" 24 | #include "spi_config.h" 25 | #include 26 | 27 | /*Functions Provided by UART API*/ 28 | /*This function initializes the SPI and sets the registers according to given data*/ 29 | void spi_init(void); 30 | 31 | /*This functions send and recieves one byte of data on the SPI bus*/ 32 | unsigned int spi_transmit_recieve(unsigned int data); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/include/spi_config.h: -------------------------------------------------------------------------------- 1 | #ifndef _SPI_CON_H 2 | #define _SPI_CON_H 3 | 4 | /* SPI PIN Definitions*/ 5 | #define SPI_MOSI PB5 6 | #define SPI_MISO PB6 7 | #define SPI_SS PB4 8 | #define SPI_SCK PB7 9 | #define SPI_DDR DDRB 10 | #define SPI_PORT PORTB 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /src/include/uart.h: -------------------------------------------------------------------------------- 1 | /** 2 | * AVRLIB: Header file for UART API configurations and function prototypes 3 | * 4 | * Copyright (C) 2019, PURANJAY MOHAN. 5 | * This file is part of AVRLIB 6 | * 7 | * AVRLIB is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * AVRLIB is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | **/ 20 | 21 | 22 | 23 | #ifndef __UART_H_ 24 | #define __UART_H__ 25 | #include "config.h" 26 | #include "uart_config.h" 27 | //Definitions for compatibility between atmega32 and atmega328p 28 | #ifdef __MCU__ATMEGA328__ 29 | #define UBRRH UBRR0H 30 | #define UBRRL UBRR0L 31 | #define UDR UDR0 32 | 33 | #define UCSRA UCSR0A 34 | #define UDRE UDRE0 35 | #define RXC RXC0 36 | 37 | #define UCSRB UCSR0B 38 | #define RXEN RXEN0 39 | #define TXEN TXEN0 40 | #define RXCIE RXCIE0 41 | 42 | #define UCSRC UCSR0C 43 | #define URSEL 44 | #define UCSZ0 UCSZ00 45 | #define UCSZ1 UCSZ01 46 | #define URSEL 47 | #define UCSRC_SELECT 0 48 | #else 49 | #define UCSRC_SELECT (1 << URSEL) 50 | #endif 51 | //end of compatibility definitions 52 | 53 | #define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1) 54 | 55 | /*Functions Provided by UART API*/ 56 | 57 | /*This Functions initializes the uart register with the given baud rate and data frame specification, they can exclsively be editied at src/lib/uart.c 58 | *This function has to be called before using any other API functions*/ 59 | void uart_init(void); 60 | 61 | /*This fuctions reads the uart buffer and returns the data as an unsigned character*/ 62 | char uart_receive(void); 63 | 64 | /*This function can be called to send data through UART as an unsigned character*/ 65 | void uart_send(char data); 66 | 67 | /*This function can be called to send a string through the UART by providing the pointer to the string as the parameter.*/ 68 | void uart_send_string(char* StringPtr); 69 | 70 | /*This function can be called to receive a string through the UART by providing the pointer to the place where the string has to be stored and also the length of the string.*/ 71 | void uart_receive_string(char* StringPtr, unsigned int len); 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /src/include/uart_config.h: -------------------------------------------------------------------------------- 1 | #ifndef __UART_CONFIG_H 2 | #define __UART_CONFIG_H 3 | 4 | #define BAUDRATE 9600 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /src/lib/gpio.c: -------------------------------------------------------------------------------- 1 | #include "gpio.h" 2 | #include 3 | #include 4 | void gpio_set_mode_output(char port, unsigned int gpio) 5 | { 6 | switch(port) 7 | { 8 | #ifndef __MCU__ATMEGA328__ 9 | case 'A': 10 | DDRA |= (1<>gpio; 142 | break; 143 | #endif 144 | case 'B': 145 | port = PINB; 146 | port &= (1<>gpio; 148 | break; 149 | case 'C': 150 | port = PINC; 151 | port &= (1<>gpio; 153 | break; 154 | case 'D': 155 | port = PIND; 156 | port &= (1<>gpio; 158 | break;; 159 | /* case 'E': 160 | port = PINE; 161 | port &= (1<>gpio; 163 | break; 164 | case 'F': 165 | port = PINF; 166 | port &= (1<>gpio; 168 | break; 169 | */ 170 | } 171 | return value; 172 | } 173 | -------------------------------------------------------------------------------- /src/lib/i2c.c: -------------------------------------------------------------------------------- 1 | /** 2 | * AVRLIB: I2C API related functions definitions 3 | * 4 | * Copyright (C) 2019, PURANJAY MOHAN. 5 | * This file is part of I2C API LIBRARY 6 | * 7 | * AVRLIB is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * AVRLIB is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | **/ 20 | 21 | #include "i2c.h" 22 | #include 23 | #include 24 | #include 25 | 26 | void i2c_init(void) 27 | { 28 | TWBR=BITRATE(TWSR=0x00); 29 | } 30 | 31 | unsigned int i2c_start(unsigned int address) 32 | { 33 | unsigned int status; 34 | TWCR = (1<. 19 | **/ 20 | 21 | #include "i2c.h" 22 | #include "i2c_lcd.h" 23 | #include 24 | #include 25 | 26 | 27 | void i2c_lcd_send_command(unsigned char cmd) 28 | { unsigned int DATA; 29 | DATA = geti2c(cmd>>4); 30 | DATA &= ~(RS) & ~(RW); 31 | i2c_write(DATA); 32 | DATA |= (EN); 33 | i2c_write(DATA); 34 | _delay_us(1); 35 | DATA &= ~(RS) & ~(RW) & ~(EN); 36 | i2c_write(DATA); 37 | _delay_us(200); 38 | DATA = geti2c(cmd); 39 | DATA &= ~(RS) & ~(RW); 40 | i2c_write(DATA); 41 | DATA |= (EN); 42 | i2c_write(DATA); 43 | _delay_us(1); 44 | DATA &= ~(RS) & ~(RW) & ~(EN); 45 | i2c_write(DATA); 46 | _delay_ms(2); 47 | return; 48 | 49 | } 50 | 51 | 52 | 53 | void i2c_lcd_init(unsigned int addr) 54 | { 55 | i2c_init(); 56 | i2c_start(addr); 57 | _delay_ms(20); 58 | i2c_lcd_send_command(0x02); 59 | _delay_ms(1); 60 | i2c_lcd_send_command(0x28); 61 | _delay_ms(1); 62 | i2c_lcd_send_command(0x01); 63 | _delay_ms(1); 64 | i2c_lcd_send_command(0x0c); 65 | _delay_ms(1); 66 | i2c_lcd_send_command(0x06); 67 | _delay_ms(1); 68 | return; 69 | } 70 | 71 | void i2c_lcd_send_data(unsigned char data) 72 | { 73 | unsigned int DATA; 74 | DATA = geti2c(data>>4); 75 | DATA |= (RS); /* RS=1, data reg. */ 76 | DATA &=~(RW); 77 | i2c_write(DATA); 78 | DATA |= (EN); 79 | i2c_write(DATA); 80 | _delay_us(1); 81 | DATA &= ~ (EN); 82 | i2c_write(DATA); 83 | _delay_us(200); 84 | DATA = geti2c(data); /* Sending lower nibble */ 85 | DATA |= (RS); 86 | DATA &= ~(RW); 87 | i2c_write(DATA); 88 | DATA |= (EN); 89 | i2c_write(DATA); 90 | _delay_us(1); 91 | DATA &= ~ (EN); 92 | i2c_write(DATA); 93 | _delay_ms(2); 94 | return; 95 | } 96 | 97 | void i2c_lcd_write_string(char *string) 98 | { 99 | int i = 0; 100 | while(string[i] != '\0') 101 | { 102 | i2c_lcd_send_data(string[i]); 103 | _delay_ms(1); 104 | i++; 105 | } 106 | return; 107 | } 108 | 109 | void i2c_lcd_set_cursor(int row, int column) 110 | { 111 | unsigned int address=0; 112 | switch(row) 113 | { 114 | case 0: 115 | address = 128+column; 116 | break; 117 | case 1: 118 | address = 192+column; 119 | break; 120 | case 2: 121 | address = 148+column; 122 | break; 123 | case 3: 124 | address = 212+column; 125 | break; 126 | 127 | } 128 | 129 | i2c_lcd_send_command(address); 130 | 131 | return; 132 | } 133 | 134 | void i2c_lcd_clrscr(void) 135 | { 136 | i2c_lcd_send_command(CLRSCR); 137 | i2c_lcd_set_cursor(0,0); 138 | return; 139 | } 140 | 141 | void i2c_lcd_hide_cursor(void) 142 | { 143 | i2c_lcd_send_command(0x0C); 144 | return; 145 | } 146 | 147 | void i2c_lcd_show_cursor_underline() 148 | { 149 | i2c_lcd_send_command(0x0E); 150 | return; 151 | } 152 | 153 | void i2c_lcd_show_cursor_block() 154 | { 155 | i2c_lcd_send_command(0x0F); 156 | return; 157 | } 158 | -------------------------------------------------------------------------------- /src/lib/lcd.c: -------------------------------------------------------------------------------- 1 | /** 2 | * AVRLIB: LCD API related functions definitions 3 | * 4 | * Copyright (C) 2019, PURANJAY MOHAN. 5 | * This file is part of HD44780 LCD AVR LIBRARY 6 | * 7 | * AVRLIB is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * AVRLIB is distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | **/ 20 | 21 | #include "lcd.h" 22 | #include 23 | #include 24 | 25 | static int bit4 = 0; 26 | 27 | void lcd_send_command(unsigned char cmd) 28 | { 29 | if (bit4==0){ 30 | DATAPORT=cmd; 31 | _delay_ms(1); 32 | CONPORT &= ~(1<. 19 | **/ 20 | 21 | #include "spi.h" 22 | #include 23 | #include 24 | 25 | void spi_init(void) 26 | { 27 | SPI_DDR &= ~((1<. 19 | **/ 20 | 21 | #include "uart.h" 22 | #include 23 | #include 24 | 25 | void uart_init(void) 26 | { 27 | UBRRH = (uint8_t)(BAUD_PRESCALLER>>8); 28 | UBRRL = (uint8_t)(BAUD_PRESCALLER); 29 | UCSRB |= (1<