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/10/17 12:58] – [Library: Serial (UART)] azman | archive:nmk322 [2024/12/19 11:56] (current) – [Lecture Slides] azman | ||
---|---|---|---|
Line 13: | Line 13: | ||
* Lecture 1 - {{: | * Lecture 1 - {{: | ||
* Lecture 2 - {{: | * Lecture 2 - {{: | ||
+ | * Lecture 3 - {{: | ||
+ | * Lecture 4 - {{: | ||
+ | * Lecture 5 - {{: | ||
+ | * Lecture 6 - {{: | ||
+ | * Lecture 7 - {{: | ||
+ | * Lecture 8 - {{: | ||
- | ===== Codes (discussed in class) | + | ===== Library |
Introduction to using ' | Introduction to using ' | ||
+ | |||
+ | I recommend to use my include file (mcu51.h) rather than the regular (reg51.h) because it would be easier for you to use my example codes and library. I wrote and tested them using my own syntax that can be used on both Keil and SDCC compilers. Simply extract this {{: | ||
+ | |||
==== Library: Serial (UART) ==== | ==== Library: Serial (UART) ==== | ||
Line 136: | Line 145: | ||
</ | </ | ||
- | ===== Test Codes ===== | + | ==== Library: Utilities (str-int conversions) |
- | These are not meant for class - just something I use to test boards/ | + | <file c utils.h> |
+ | #ifndef __MY1UTILS_H__ | ||
+ | #define __MY1UTILS_H__ | ||
- | ==== Test Code for Base Kit ==== | + | 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 = that/10; | ||
+ | if (temp) { | ||
+ | that %= 10; | ||
+ | cstr[size++] | ||
+ | } else if (size) | ||
+ | cstr[size++] | ||
+ | cstr[size++] = that + 0x30; | ||
+ | cstr[size] = 0x0; | ||
+ | return size; | ||
+ | } | ||
- | <file c basetest.c> | + | unsigned char int8_2cstr(char* cstr, signed char that) { |
- | /*----------------------------------------------------------------------------*/ | + | if (that<0) { |
- | /* override default tick values */ | + | that = -that; |
- | #define TIMER_TICK_LEN TIMER_VAL50MS | + | *cstr = ' |
- | #define TIMER_TICK_CNT TIMER_LOOP_1S | + | cstr++; |
- | / | + | } |
- | #include " | + | return uint8_2cstr(cstr, |
- | #include " | + | } |
- | #include " | + | |
- | /*----------------------------------------------------------------------------*/ | + | unsigned char uint16_2cstr(char* cstr, unsigned int that) { |
- | void main(void) { | + | unsigned char size, temp; |
- | unsigned char loop, mask, seg7[] = { _7SEGNUM_CC_ }; | + | unsigned int idiv; |
- | P1MODE1(); // better pullup current | + | size = 0; idiv = 10000; |
- | loop = 0; mask = (!P2_7)?0xff:0x00; | + | while |
- | P1 = seg7[loop] ^ mask; | + | temp = (unsigned char)(that/ |
- | timer_init(); | + | if (temp) { |
- | timer_tick_exec(); | + | that %= idiv; |
- | while (1) { | + | cstr[size++] = temp + 0x30; |
- | P2_0 = !P3_7; | + | } else if (size) |
- | P2_1 = !P3_6; | + | cstr[size++] = 0x30; |
- | P2_2 = !P3_5; | + | idiv /= 10; |
- | P2_3 = !P3_4; | + | } |
- | if (timer_ticked()) { | + | cstr[size++] = that + 0x30; |
- | loop++; if (loop==10) loop = 0; | + | cstr[size] = 0x0; |
- | mask = (!P2_7)?0xff:0x00; | + | return size; |
- | P1 = seg7[loop] | + | } |
- | timer_tick00(); | + | |
+ | unsigned char int16_2cstr(char* cstr, int that) { | ||
+ | if (that<0) { | ||
+ | that = -that; | ||
+ | *cstr = ' | ||
+ | cstr++; | ||
+ | } | ||
+ | return uint16_2cstr(cstr,(unsigned int)that); | ||
+ | } | ||
+ | |||
+ | unsigned char cstr_2uint8(char* pstr, unsigned char* pval) { | ||
+ | unsigned char loop; | ||
+ | *pval | ||
+ | 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__ */ | ||
+ | </ | ||
+ | |||
+ | ==== Library: Keypad ==== | ||
+ | |||
+ | <file c kpad.h> | ||
+ | #ifndef __MY1KPAD_H__ | ||
+ | #define __MY1KPAD_H__ | ||
+ | |||
+ | /* 4x4 keypad interface {R0, | ||
+ | /* 4x3 keypad interface {C1, | ||
+ | |||
+ | /** DefaultPins: | ||
+ | #ifndef KEY_DATA | ||
+ | #define KEY_DATA P1 | ||
+ | #endif | ||
+ | #ifndef _ROWS_AT_UPPER_ | ||
+ | #define ROWS_FLAG 0x01 | ||
+ | #define COLS_FLAG 0x10 | ||
+ | #else | ||
+ | #define ROWS_FLAG 0x10 | ||
+ | #define COLS_FLAG 0x01 | ||
+ | #endif | ||
+ | |||
+ | /** no-key indicator for keypad */ | ||
+ | #define KEY_NOT_CODE 0x10 | ||
+ | |||
+ | unsigned char key_scan(void) { | ||
+ | /** scan for key press */ | ||
+ | unsigned char irow, icol, mask, test; | ||
+ | mask = ROWS_FLAG; | ||
+ | for (irow=0; | ||
+ | KEY_DATA = ~mask; test = COLS_FLAG; | ||
+ | for (icol=0; | ||
+ | if ((KEY_DATA& | ||
+ | while ((KEY_DATA& | ||
+ | if (icol==3) return irow+0x0A; | ||
+ | if (irow==0) return icol+1; | ||
+ | if (irow==1) return icol+4; | ||
+ | if (irow==2) return icol+7; | ||
+ | if (icol==1) return 0; | ||
+ | return (icol>> | ||
+ | } | ||
+ | test <<= 1; | ||
+ | } | ||
+ | mask <<= 1; | ||
+ | } | ||
+ | return KEY_NOT_CODE; | ||
+ | } | ||
+ | |||
+ | unsigned char key_wait(void) { | ||
+ | /** wait for key press */ | ||
+ | unsigned char scan; | ||
+ | while ((scan=key_scan())==KEY_NOT_CODE); | ||
+ | return scan; | ||
} | } | ||
- | / | ||
</ | </ | ||
archive/nmk322.1729141120.txt.gz · Last modified: by azman