From e689500b11c870747b27f099097d03d464db278d Mon Sep 17 00:00:00 2001 From: matiasus Date: Thu, 29 Apr 2021 08:47:08 +0200 Subject: [PATCH] Update comments --- lib/font.c | 3 +- lib/font.h | 2 +- lib/ssd1306.c | 35 ++++++++------- lib/twi.c | 24 +++++----- lib/twi.h | 121 ++++++++++++++++++++++++++------------------------ main.hex | 112 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 211 insertions(+), 86 deletions(-) create mode 100644 main.hex diff --git a/lib/font.c b/lib/font.c index 90491df..481b927 100644 --- a/lib/font.c +++ b/lib/font.c @@ -8,7 +8,7 @@ * @author Marian Hrinko * @datum 07.10.2020 * @file font.c - * @tested AVR Atmega16 + * @tested AVR Atmega16, Atmega8, Atmega328 * * @depend * ---------------------------------------------------------------+ @@ -19,7 +19,6 @@ const uint8_t FONTS[][5] PROGMEM = { { 0x00, 0x00, 0x00, 0x00, 0x00 }, // 20 space { 0x81, 0x81, 0x18, 0x81, 0x81 }, // 21 ! -// { 0x00, 0x00, 0x5f, 0x00, 0x00 }, // 21 ! { 0x00, 0x07, 0x00, 0x07, 0x00 }, // 22 " { 0x14, 0x7f, 0x14, 0x7f, 0x14 }, // 23 # { 0x24, 0x2a, 0x7f, 0x2a, 0x12 }, // 24 $ diff --git a/lib/font.h b/lib/font.h index 0fec876..0130b8e 100644 --- a/lib/font.h +++ b/lib/font.h @@ -8,7 +8,7 @@ * @author Marian Hrinko * @datum 07.10.2020 * @file font.h - * @tested AVR Atmega16 + * @tested AVR Atmega16, Atmega8, Atmega328 * * @depend * ---------------------------------------------------------------+ diff --git a/lib/ssd1306.c b/lib/ssd1306.c index cbd647c..c540c44 100644 --- a/lib/ssd1306.c +++ b/lib/ssd1306.c @@ -7,12 +7,15 @@ * * @author Marian Hrinko * @datum 06.10.2020 - * @file ssd1306.h - * @tested AVR Atmega16 + * @file ssd1306.c + * @tested AVR Atmega16, ATmega8, Atmega328 * * @depend font.h, twi.h * ---------------------------------------------------------------+ + * @usage Basic Setup for OLED */ + + #include #include "font.h" #include "twi.h" @@ -161,7 +164,7 @@ unsigned short int indexPage = START_PAGE_ADDR; * * @return char */ -char SSD1306_Init(void) +char SSD1306_Init (void) { // variables const uint8_t *commands = INIT_SSD1306; @@ -234,7 +237,7 @@ char SSD1306_Init(void) * * @return char */ -char SSD1306_Send_StartAndSLAW(void) +char SSD1306_Send_StartAndSLAW (void) { // init status char status = INIT_STATUS; @@ -268,7 +271,7 @@ char SSD1306_Send_StartAndSLAW(void) * * @return char */ -char SSD1306_Send_Command(char command) +char SSD1306_Send_Command (char command) { // init status char status = INIT_STATUS; @@ -302,7 +305,7 @@ char SSD1306_Send_Command(char command) * * @return char */ -char SSD1306_NormalScreen(void) +char SSD1306_NormalScreen (void) { // init status char status = INIT_STATUS; @@ -336,7 +339,7 @@ char SSD1306_NormalScreen(void) * * @return char */ -char SSD1306_InverseScreen(void) +char SSD1306_InverseScreen (void) { // init status char status = INIT_STATUS; @@ -370,7 +373,7 @@ char SSD1306_InverseScreen(void) * * @return char */ -char SSD1306_UpdateScreen(void) +char SSD1306_UpdateScreen (void) { // init status char status = INIT_STATUS; @@ -404,7 +407,7 @@ char SSD1306_UpdateScreen(void) * * @return char */ -char SSD1306_ClearScreen(void) +char SSD1306_ClearScreen (void) { // init status char status = INIT_STATUS; @@ -466,7 +469,7 @@ char SSD1306_ClearScreen(void) * * @return char */ -char SSD1306_SetPosition(char x, char y) +char SSD1306_SetPosition (char x, char y) { // variables char status = INIT_STATUS; @@ -553,7 +556,7 @@ char SSD1306_SetPosition(char x, char y) * * @return char */ -char SSD1306_UpdTxtPosition(void) +char SSD1306_UpdTxtPosition (void) { // init status char status = INIT_STATUS; @@ -588,7 +591,7 @@ char SSD1306_UpdTxtPosition(void) * * @return void */ -char SSD1306_DrawChar(char character) +char SSD1306_DrawChar (char character) { // variables uint8_t idxCol = 0; @@ -659,7 +662,7 @@ char SSD1306_DrawChar(char character) * * @return void */ -char SSD1306_SendByte(char data) +char SSD1306_SendByte (char data) { // init status char status = INIT_STATUS; @@ -708,7 +711,7 @@ char SSD1306_SendByte(char data) * * @return void */ -char SSD1306_SendBytes(char data, char length) +char SSD1306_SendBytes (char data, char length) { // index unsigned short int i = 0; @@ -766,7 +769,7 @@ char SSD1306_SendBytes(char data, char length) * * @return void */ -char SSD1306_DrawString(char *str) +char SSD1306_DrawString (char *str) { // init status char status = INIT_STATUS; @@ -797,7 +800,7 @@ char SSD1306_DrawString(char *str) * * @return void */ -char SSD1306_DrawLineHorizontal(char x, char y, char len) +char SSD1306_DrawLineHorizontal (char x, char y, char len) { char page = 0; char pixel = 0; diff --git a/lib/twi.c b/lib/twi.c index d26c1f6..4ef2fb0 100644 --- a/lib/twi.c +++ b/lib/twi.c @@ -1,18 +1,22 @@ /** - * ---------------------------------------------------+ + * ---------------------------------------------------------------+ * @desc Two Wire Interface / I2C Communication - * ---------------------------------------------------+ + * ---------------------------------------------------------------+ * Copyright (C) 2020 Marian Hrinko. * Written by Marian Hrinko (mato.hrinko@gmail.com) * * @author Marian Hrinko * @datum 06.09.2020 * @file twi.c - * @tested AVR Atmega16 - * --------------------------------------------------- + * @tested AVR Atmega16, ATmega8, Atmega328 + * + * @depend + * ---------------------------------------------------------------+ + * @usage Master Transmit Operation */ // include libraries +#include #include "twi.h" /** @@ -83,12 +87,12 @@ char TWI_MT_Send_SLAW(char address) // wait till flag set TWI_WAIT_TILL_TWINT_IS_SET(); - // find + // test if SLA with WRITE acknowledged if (TWI_STATUS != TWI_MT_SLAW_ACK) { // return status return TWI_STATUS; } - // return found device address + // success return SUCCESS; } @@ -109,12 +113,12 @@ char TWI_MT_Send_Data(char data) // wait till flag set TWI_WAIT_TILL_TWINT_IS_SET(); - // find + // test if data acknowledged if (TWI_STATUS != TWI_MT_DATA_ACK) { // return status return TWI_STATUS; } - // return found device address + // success return SUCCESS; } @@ -135,12 +139,12 @@ char TWI_MR_Send_SLAR(char address) // wait till flag set TWI_WAIT_TILL_TWINT_IS_SET(); - // find + // test if SLA with READ acknowledged if (TWI_STATUS != TWI_MR_SLAR_ACK) { // return status return TWI_STATUS; } - // return found device address + // success return SUCCESS; } diff --git a/lib/twi.h b/lib/twi.h index b1968d8..626d38a 100644 --- a/lib/twi.h +++ b/lib/twi.h @@ -1,76 +1,46 @@ /** - * ---------------------------------------------------+ + * ---------------------------------------------------------------+ * @desc Two Wire Interface / I2C Communication - * ---------------------------------------------------+ + * ---------------------------------------------------------------+ * Copyright (C) 2020 Marian Hrinko. * Written by Marian Hrinko (mato.hrinko@gmail.com) * * @author Marian Hrinko * @datum 06.09.2020 * @file twi.h - * @tested AVR Atmega16 - * --------------------------------------------------- + * @tested AVR Atmega16, ATmega8, Atmega328 + * + * @depend + * ---------------------------------------------------------------+ + * @usage Basic Master Transmit Operation */ -#include -#include - #ifndef __TWI_H__ #define __TWI_H__ - // define clock - #if defined(__AVR_ATmega8__) - #define _FCPU 8000000 - #elif defined(__AVR_ATmega16__) - #define _FCPU 16000000 - #endif - // define register for TWI communication - #if defined(__AVR_ATmega16__) - #define TWI_TWAR TWAR // TWI (Slave) Address Register - #define TWI_TWBR TWBR // TWI Bit Rate Register - #define TWI_TWDR TWDR // TWI Data Register - #define TWI_TWCR TWCR // TWI Control Register - #define TWI_TWSR TWSR // TWI Status Register + // ------------------------------------------- + #if defined(__AVR_ATmega16__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__) + + #define TWI_TWAR TWAR // TWI (Slave) Address Register + #define TWI_TWBR TWBR // TWI Bit Rate Register + #define TWI_TWDR TWDR // TWI Data Register + #define TWI_TWCR TWCR // TWI Control Register + #define TWI_TWSR TWSR // TWI Status Register + #endif - // TWI CLK frequency - // @param TWBR - // @param Prescaler - // TWPS1 TWPS0 - PRESCALER - // 0 0 - 1 - // 0 1 - 4 - // 1 0 - 16 - // 1 1 - 64 - #define TWI_FREQ(BIT_RATE, PRESCALER) { TWI_TWBR = BIT_RATE; TWI_TWSR |= (TWI_TWSR & 0x03) | PRESCALER; } + // Success + // ------------------------------------------- + #ifndef SUCCESS + #define SUCCESS 0 + #endif - // TWI start condition - // (1 << TWEN) - TWI Enable - // (1 << TWINT) - TWI Interrupt Flag - must be cleared by set - // (1 << TWSTA) - TWI Start - #define TWI_START() { TWI_TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWSTA); } - - // TWI enable - // (1 << TWEN) - TWI Enable - // (1 << TWINT) - TWI Interrupt Flag - must be cleared by set - #define TWI_ENABLE() { TWI_TWCR = (1 << TWEN) | (1 << TWINT); } - - // TWI stop condition - // (1 << TWEN) - TWI Enable - // (1 << TWINT) - TWI Interrupt Flag - must be cleared by set - // (1 << TWSTO) - TWI Stop - #define TWI_STOP() { TWI_TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWSTO); } - - // TWI test if TWINT Flag is set - #define TWI_WAIT_TILL_TWINT_IS_SET() { while (!(TWI_TWCR & (1 << TWINT))); } - - // TWI status mask - #define TWI_STATUS (TWI_TWSR & 0xF8) - - // success return value - #define SUCCESS 0 - // success return value - #define ERROR 1 + // Error + // ------------------------------------------- + #ifndef ERROR + #define ERROR 1 + #endif // ++++++++++++++++++++++++++++++++++++++++++ // @@ -113,9 +83,46 @@ #define TWI_ST_DATA_ACK 0xB8 // Data byte in TWDR has been transmitted; ACK has been received #define TWI_ST_DATA_NACK 0xC0 // Data byte in TWDR has been transmitted; NOT ACK has been received #define TWI_ST_DATA_LOST_ACK 0xC8 // Last data byte in TWDR has been transmitted (TWEA = '0'); ACK has been received + + // TWI CLK frequency + // ------------------------------------------- + // @param TWBR + // @param Prescaler + // TWPS1 TWPS0 - PRESCALER + // 0 0 - 1 + // 0 1 - 4 + // 1 0 - 16 + // 1 1 - 64 + #define TWI_FREQ(BIT_RATE, PRESCALER) { TWI_TWBR = BIT_RATE; TWI_TWSR |= (TWI_TWSR & 0x03) | PRESCALER; } + + // TWI start condition + // ------------------------------------------- + // (1 << TWEN) - TWI Enable + // (1 << TWINT) - TWI Interrupt Flag - must be cleared by set + // (1 << TWSTA) - TWI Start + #define TWI_START() { TWI_TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWSTA); } + + // TWI stop condition + // ------------------------------------------- + // (1 << TWEN) - TWI Enable + // (1 << TWINT) - TWI Interrupt Flag - must be cleared by set + // (1 << TWSTO) - TWI Stop + #define TWI_STOP() { TWI_TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWSTO); } + + // TWI enable + // ------------------------------------------- + // (1 << TWEN) - TWI Enable + // (1 << TWINT) - TWI Interrupt Flag - must be cleared by set + #define TWI_ENABLE() { TWI_TWCR = (1 << TWEN) | (1 << TWINT); } + + // TWI test if TWINT Flag is set + #define TWI_WAIT_TILL_TWINT_IS_SET() { while (!(TWI_TWCR & (1 << TWINT))); } + + // TWI status mask + #define TWI_STATUS ( TWI_TWSR & 0xF8 ) /** - * @desc TWI init - initialise communication + * @desc TWI init * * @param void * diff --git a/main.hex b/main.hex new file mode 100644 index 0000000..f75dfb2 --- /dev/null +++ b/main.hex @@ -0,0 +1,112 @@ +:100000000C9433010C9450010C9450010C94500149 +:100010000C9450010C9450010C9450010C9450011C +:100020000C9450010C9450010C9450010C9450010C +:100030000C9450010C9450010C9450010C945001FC +:100040000C9450010C9450010C9450010C945001EC +:100050000C9450010000000000818118818100078C +:10006000000700147F147F14242A7F2A1223130808 +:10007000646236495522500005030000001C2241ED +:10008000000041221C0014083E081408083E08081D +:1000900000503000000808080808006060000020D8 +:1000A000100804023E5149453E00427F4000426133 +:1000B0005149462141454B311814127F10274545BF +:1000C00045393C4A494930017109050336494949D6 +:1000D00036064949291E0036360000005636000013 +:1000E00008142241001414141414004122140802AC +:1000F00001510906324979413E7E1111117E7F4935 +:100100004949363E414141227F4141221C7F4949B4 +:1001100049417F090909013E4149497A7F08080898 +:100120007F00417F41002040413F017F0814224170 +:100130007F404040407F020C027F7F0408107F3EDA +:100140004141413E7F090909063E4151215E7F0937 +:10015000192946464949493101017F01013F404083 +:10016000403F1F2040201F3F4038403F6314081489 +:100170006307087008076151494543007F4141000A +:1001800002040810200041417F00040201020440E3 +:1001900040404040000102040020545454787F48FD +:1001A0004444383844444420384444487F38545404 +:1001B0005418087E0901020C5252523E7F08040472 +:1001C0007800447D40002040443D007F10284400DA +:1001D00000417F40007C041804787C0804047838CF +:1001E000444444387C14141408081414147C7C0807 +:1001F0000404084854545420043F4440203C4040E8 +:10020000207C1C2040201C3C4030403C44281028CE +:10021000440C5050503C4464544C44000836410057 +:1002200000007F0000004136080010080810080098 +:10023000000000001200AE01A83F012000022100D2 +:100240007F02220007004001D30000A100C801DAAC +:100250001201817F00A400A601D58001D9C201DB73 +:1002600020018D1400AF11241FBECFE5D4E0DEBF06 +:10027000CDBF10E0A0E6B0E0ECECF6E002C00590E7 +:100280000D92A637B107D9F720E0A6E7B0E001C08C +:100290001D92AA37B207E1F70E9449030C94640348 +:1002A0000C94000084E180B981B191B182708160C9 +:1002B000892B81B9089581B1877581B984EA86BF98 +:1002C00006B607FEFDCF81B1887F883039F081B155 +:1002D000887F803119F081B1887F089580E008958A +:1002E000880F83B984E886BF06B607FEFDCF81B1CB +:1002F000887F883119F081B1887F089580E0089562 +:1003000083B984E886BF06B607FEFDCF81B1887F3A +:10031000883219F081B1887F089580E00895880FB0 +:10032000990B816083B984E886BF06B607FEFDCFCE +:1003300081B1887F803419F081B1887F089580E091 +:10034000089584E986BF08950E945B01811103C06E +:100350008CE30C9470010895CF93C82F80E80E941D +:100360008001811104C08C2FCF910C948001CF911A +:100370000895FF920F931F93CF93DF93E4E3F2E08E +:10038000C4910E9452010E94A40181111FC0D0E0BB +:10039000E5E3F2E02097B9F0F4908F010E5F1F4F74 +:1003A000319684910E94AC01811110C0F801FF20A8 +:1003B00041F0F80184910E94AC01FA940F5F1F4F45 +:1003C000F3CF2197E7CF0E94A10180E0DF91CF9189 +:1003D0001F910F91FF9008950E94A401811103C005 +:1003E00086EA0C94AC0108950E94A401811103C017 +:1003F00087EA0C94AC0108950E94A401811103C006 +:100400008FEA0C94AC010895CF93DF93D82FC62FB9 +:100410000E94A40181112FC081E20E94AC018111D0 +:100420002AC08D2F0E94AC01811125C08FE70E9448 +:10043000AC01811120C08D2FDD0F990B90937900B5 +:100440008093780082E20E94AC01811114C08C2F4D +:100450000E94AC0181110FC087E00E94AC018111A4 +:100460000AC06C2FCC0F770B7093770060937600E7 +:100470000E94A10180E0DF91CF910895CF93DF9397 +:1004800060E080E00E94040281111CC00E94A4016F +:10049000811118C080E40E948001811113C0C0E066 +:1004A000D0E08091600090916100C817D90738F4BE +:1004B00080E00E948001811105C02196F2CF0E9448 +:1004C000A10180E0DF91CF91089580917800909113 +:1004D0007900069680389105A0F0609176007091C1 +:1004E00077006730710578F41092790010927800E7 +:1004F0006F5F7F4F709377006093760080E00C947D +:10050000040280E0089581E008950F931F93CF9334 +:10051000DF93C82F0E946502811131C00E94A4019F +:1005200081112EC080E40E948001811129C085E0E4 +:10053000C802E001112400E010E0C05AD109FE0118 +:10054000E00FF11FEC5AFF4F84910E94800181114E +:1005500017C00F5F1F4F0530110589F70E948001FA +:1005600081110EC080917800909179000696909349 +:100570007900809378000E94A10180E001C081E0B1 +:10058000DF91CF911F910F910895CF93C82F0E94B3 +:10059000A401811116C080E40E948001811111C064 +:1005A0008C2F0E94800181110CC080917800909165 +:1005B0007900019690937900809378000E94A101C0 +:1005C00080E0CF910895FF920F931F93CF93DF9315 +:1005D000F82EC62F0E94A401811128C080E40E9439 +:1005E0008001811123C000E010E00C2E000CDD0B17 +:1005F0000C171D07C0F480917800909179008F3717 +:10060000910588F48F2D0E9480010F5F1F4F81118B +:100610000DC08091780090917900019690937900B7 +:1006200080937800E5CF0E94A10180E0DF91CF9117 +:100630001F910F91FF900895CF93DF93EC01899163 +:10064000882321F00E9485028823C9F3DF91CF918E +:100650000895CF93DF93C62FD42F67FF02C067E0C2 +:100660006C0F6595659565950E9404029C2F97789F +:1006700097FF03C09150986F9F5F6D2F81E001C07D +:10068000880F9A95EAF70E94E30280E0DF91CF910C +:1006900008950E94B9010E943E0243E764E084E0AD +:1006A0000E94290361E085E00E94040282E690E056 +:1006B0000E941C0343E762E184E00E9429030E9438 +:0C06C000FC0180E090E00895F894FFCF6A +:1006CC00000453534431333036204F4C45442044BE +:0606DC0052495645520090 +:00000001FF