archive:nmk322
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| archive:nmk322 [2024/11/04 12:23] – [Library: Timer] azman | archive:nmk322 [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== NMK32203 - Microcontroller ====== | ||
| - | |||
| - | // | ||
| - | |||
| - | This course is // | ||
| - | |||
| - | ===== Announcements ===== | ||
| - | |||
| - | [20241017] Updating this for 202425s1 Academic Session. | ||
| - | |||
| - | ===== Lecture Slides ===== | ||
| - | |||
| - | * Lecture 1 - {{: | ||
| - | * Lecture 2 - {{: | ||
| - | |||
| - | ===== Codes (discussed in class) ===== | ||
| - | |||
| - | Introduction to using ' | ||
| - | ==== Library: Serial (UART) ==== | ||
| - | |||
| - | <file c uart.h> | ||
| - | #ifndef __MY1UART_H__ | ||
| - | #define __MY1UART_H__ | ||
| - | |||
| - | /* note: this is NOT compatible with keil's printf */ | ||
| - | void uart_init(void) { | ||
| - | SCON = 0x50; | ||
| - | TMOD &= 0x0F; | ||
| - | TMOD |= 0x20; | ||
| - | TH1 = 253; | ||
| - | TR1 = 1; | ||
| - | } | ||
| - | |||
| - | void uart_send(unsigned char sdat) { | ||
| - | SBUF = sdat; | ||
| - | while (TI==0); | ||
| - | TI = 0; | ||
| - | } | ||
| - | |||
| - | unsigned char uart_read(void) { | ||
| - | unsigned char rdat; | ||
| - | while (RI==0); | ||
| - | rdat = SBUF; | ||
| - | RI = 0; | ||
| - | return rdat; | ||
| - | } | ||
| - | |||
| - | void uart_puts(char* text) { | ||
| - | while (*text) { | ||
| - | uart_send(*text); | ||
| - | text++; | ||
| - | } | ||
| - | } | ||
| - | |||
| - | #endif /* __MY1UART_H__ */ | ||
| - | </ | ||
| - | |||
| - | ==== Library: Text LCD ==== | ||
| - | |||
| - | <file c tlcd.h> | ||
| - | #ifndef __MY1TLCD_H__ | ||
| - | #define __MY1TLCD_H__ | ||
| - | |||
| - | #define LCD_DATA P2 | ||
| - | sbit LCD_RS = P0^7; | ||
| - | sbit LCD_RW = P0^6; | ||
| - | sbit LCD_EN = P0^5; | ||
| - | |||
| - | void tlcd_delay(unsigned char step) { | ||
| - | unsigned int loop; | ||
| - | do { | ||
| - | loop = 1000; while (--loop); | ||
| - | } while (--step); | ||
| - | } | ||
| - | |||
| - | void tlcd_write(unsigned char cdat) { | ||
| - | LCD_RS = 1; | ||
| - | LCD_RW = 0; | ||
| - | LCD_DATA = cdat; | ||
| - | LCD_EN = 1; | ||
| - | LCD_EN = 0; | ||
| - | tlcd_delay(1); | ||
| - | } | ||
| - | |||
| - | void tlcd_cmd(unsigned char ccmd) { | ||
| - | LCD_RS = 0; | ||
| - | LCD_RW = 0; | ||
| - | LCD_DATA = ccmd; | ||
| - | LCD_EN = 1; | ||
| - | LCD_EN = 0; | ||
| - | tlcd_delay(2); | ||
| - | } | ||
| - | |||
| - | void tlcd_init(void) { | ||
| - | tlcd_cmd(0x38); | ||
| - | tlcd_cmd(0x38); | ||
| - | tlcd_cmd(0x38); | ||
| - | tlcd_cmd(0x06); | ||
| - | tlcd_cmd(0x0c); | ||
| - | tlcd_cmd(0x01); | ||
| - | } | ||
| - | |||
| - | void tlcd_puts(char* text) { | ||
| - | while (*text) { | ||
| - | tlcd_write(*text); | ||
| - | text++; | ||
| - | } | ||
| - | } | ||
| - | |||
| - | #endif /* __MY1TLCD_H__ */ | ||
| - | </ | ||
| - | |||
| - | ==== Library: Timer ==== | ||
| - | |||
| - | <file c timer.h> | ||
| - | #ifndef __MY1TIMER_H__ | ||
| - | #define __MY1TIMER_H__ | ||
| - | |||
| - | // useful macro! | ||
| - | #define timer0_init() { TMOD &= 0xF0; TMOD |= 0x01; } | ||
| - | #define timer0_prep(hi, | ||
| - | #define timer0_stop() { TR0 = 0; TF0 = 0; } | ||
| - | #define timer0_exec() { TR0 = 1; } | ||
| - | #define timer0_flag() { timer0_exec(); | ||
| - | #define timer0_wait(hi, | ||
| - | #define timer0_null() { TH0 = 0; TL0 = 0; } | ||
| - | #define timer0_read() ((unsigned int)TH0<< | ||
| - | |||
| - | void timer0_delayms(unsigned int step) { | ||
| - | do { | ||
| - | timer0_wait(0xfc, | ||
| - | } while (--step); | ||
| - | } | ||
| - | |||
| - | #endif /* __MY1TIMER_H__ */ | ||
| - | </ | ||
| - | |||
| - | ==== Library: Utilities (str-int conversions) ==== | ||
| - | |||
| - | <file c utils.h> | ||
| - | #ifndef __MY1UTILS_H__ | ||
| - | #define __MY1UTILS_H__ | ||
| - | |||
| - | unsigned char uint8_2cstr(char* cstr, unsigned char that) { | ||
| - | unsigned char size, temp; | ||
| - | size = 0; | ||
| - | temp = that/100; | ||
| - | if (temp) { | ||
| - | that %= 100; | ||
| - | cstr[size++] = temp + 0x30; | ||
| - | } | ||
| - | temp = that/10; | ||
| - | if (temp) { | ||
| - | that %= 10; | ||
| - | cstr[size++] = temp + 0x30; | ||
| - | } else if (size) | ||
| - | cstr[size++] = 0x30; | ||
| - | cstr[size++] = that + 0x30; | ||
| - | cstr[size] = 0x0; | ||
| - | return size; | ||
| - | } | ||
| - | |||
| - | unsigned char int8_2cstr(char* cstr, signed char that) { | ||
| - | if (that<0) { | ||
| - | that = -that; | ||
| - | *cstr = ' | ||
| - | cstr++; | ||
| - | } | ||
| - | return uint8_2cstr(cstr, | ||
| - | } | ||
| - | |||
| - | unsigned char uint16_2cstr(char* cstr, unsigned int that) { | ||
| - | unsigned char size, temp; | ||
| - | unsigned int idiv; | ||
| - | size = 0; idiv = 10000; | ||
| - | while (idiv> | ||
| - | temp = (unsigned char)(that/ | ||
| - | if (temp) { | ||
| - | that %= idiv; | ||
| - | cstr[size++] = temp + 0x30; | ||
| - | } else if (size) | ||
| - | cstr[size++] = 0x30; | ||
| - | idiv /= 10; | ||
| - | } | ||
| - | cstr[size++] = that + 0x30; | ||
| - | cstr[size] = 0x0; | ||
| - | return size; | ||
| - | } | ||
| - | |||
| - | unsigned char int16_2cstr(char* cstr, int that) { | ||
| - | if (that<0) { | ||
| - | that = -that; | ||
| - | *cstr = ' | ||
| - | cstr++; | ||
| - | } | ||
| - | return uint16_2cstr(cstr, | ||
| - | } | ||
| - | |||
| - | unsigned char cstr_2uint8(char* pstr, unsigned char* pval) { | ||
| - | unsigned char loop; | ||
| - | *pval = 0; loop = 0; | ||
| - | while (pstr[loop]) { | ||
| - | if (pstr[loop]< | ||
| - | loop = 0; | ||
| - | break; | ||
| - | } | ||
| - | *pval = (*pval*10) + (pstr[loop]-0x30); | ||
| - | loop++; | ||
| - | } | ||
| - | return loop; | ||
| - | } | ||
| - | |||
| - | unsigned char cstr_2uint16(char* pstr, unsigned int* pval) { | ||
| - | unsigned char loop; | ||
| - | *pval = 0; loop = 0; | ||
| - | while (pstr[loop]) { | ||
| - | if (pstr[loop]< | ||
| - | loop = 0; | ||
| - | break; | ||
| - | } | ||
| - | *pval = (*pval*10) + (pstr[loop]-0x30); | ||
| - | loop++; | ||
| - | } | ||
| - | return loop; | ||
| - | } | ||
| - | |||
| - | unsigned char cstr_2int16(char* pstr, int* pval) { | ||
| - | unsigned char loop, skip; | ||
| - | skip = (pstr[0]==' | ||
| - | loop = cstr_2uint16(& | ||
| - | if (skip) *pval = -(*pval); | ||
| - | return loop; | ||
| - | } | ||
| - | |||
| - | #endif /** __MY1UTILS_H__ */ | ||
| - | </ | ||
archive/nmk322.1730694224.txt.gz · Last modified: by azman
