notes:code8051
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
notes:code8051 [2024/10/17 13:02] – [Codes for NMK322] azman | notes:code8051 [2024/11/11 11:50] (current) – removed azman | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== 8051 Codes ====== | ||
- | |||
- | Example codes (in C) for the classical Intel-8051 (mcu51) microcontroller core. Using my1code51 library. Tested using sdcc open source compiler and stc12 device. | ||
- | |||
- | ===== Codes for NMK322 ===== | ||
- | |||
- | ++++ Testing IR module and HC-SR04 ultrasonic sensor module. | | ||
- | <file c test0.c> | ||
- | / | ||
- | #include " | ||
- | #include " | ||
- | #define FPSIZE 2 | ||
- | #include " | ||
- | #define _LCD_4BIT_INTERFACE_ | ||
- | /** DB4-DB7 => P0 (UPPER NIBBLE) */ | ||
- | /** E => P0.2 : NEEDS -ve EDGE */ | ||
- | /** R/W => P0.1 : 0=WR 1=RD */ | ||
- | /** RS => P0.0 : 0=CMD 1=DAT */ | ||
- | #include " | ||
- | / | ||
- | /** | ||
- | * Testing IR module and HC-SR04 ultrasonic sensor module | ||
- | * - display on both uart and lcd | ||
- | **/ | ||
- | / | ||
- | MY1SBIT(IRMOD, | ||
- | MY1SBIT(TRIG, | ||
- | MY1SBIT(ECHO, | ||
- | / | ||
- | MY1SBIT(TEST_IR, | ||
- | MY1SBIT(TEST_HC, | ||
- | / | ||
- | #define FLAG_IR 0x01 | ||
- | #define FLAG_HC 0x02 | ||
- | / | ||
- | void main(void) { | ||
- | unsigned char wait; | ||
- | unsigned int tval,loop, flag; | ||
- | float fval, dist; | ||
- | char buff[16]; | ||
- | uart_init(); | ||
- | uart_puts(" | ||
- | lcd_init(); | ||
- | lcd_goto_line1(); | ||
- | lcd_puts(" | ||
- | timer_init(); | ||
- | flag = 0; | ||
- | while (1) { | ||
- | if (TEST_IR==0) { | ||
- | if ((flag& | ||
- | lcd_goto_line1(); | ||
- | lcd_puts(" | ||
- | uart_puts(" | ||
- | flag = FLAG_IR; | ||
- | } | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | uart_puts(" | ||
- | while (IRMOD); // outputs logic low when obstacle detected | ||
- | lcd_puts(" | ||
- | uart_puts(" | ||
- | while (!IRMOD); | ||
- | lcd_puts(" | ||
- | uart_puts(" | ||
- | for (loop=65000; | ||
- | } | ||
- | if (TEST_HC==0) { | ||
- | if ((flag& | ||
- | lcd_goto_line1(); | ||
- | lcd_puts(" | ||
- | uart_puts(" | ||
- | flag = FLAG_HC; | ||
- | } | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | lcd_goto_line2(); | ||
- | timer_prep(0); | ||
- | TRIG = 1; | ||
- | for (wait=10; | ||
- | TRIG = 0; | ||
- | while (!ECHO); timer_exec(); | ||
- | while (ECHO); timer_stop(); | ||
- | tval = ((unsigned int)TH0<< | ||
- | fval = (float)tval * 1.085; // in us | ||
- | dist = fval / 58.0; // in cm | ||
- | lcd_puts(" | ||
- | uart_puts(" | ||
- | float2str(buff, | ||
- | lcd_puts(buff); | ||
- | lcd_puts(" | ||
- | uart_puts(buff); | ||
- | uart_puts(" | ||
- | for (loop=65000; | ||
- | } | ||
- | } | ||
- | } | ||
- | / | ||
- | </ | ||
- | ++++ | ||
- | |||
- | ++++ Testing HC06 bluetooth module , TowerPro MG996R servo & Text LCD. | | ||
- | <file c nmk322_test1.c> | ||
- | / | ||
- | /** | ||
- | * Testing HC06 bluetooth module , TowerPro MG996R servo & Text LCD | ||
- | * - note: servo pin (1|orange: | ||
- | **/ | ||
- | / | ||
- | #define BTNAME " | ||
- | #define BTPASS " | ||
- | #include " | ||
- | #include " | ||
- | #include " | ||
- | #include " | ||
- | / | ||
- | /** interface as defined in nmk322 lab module */ | ||
- | #define _LCD_8BIT_INTERFACE_ | ||
- | #define _LCD_PINS_DEFINED_ | ||
- | MY1SFR(LCD_DATA, | ||
- | MY1SBIT(LCD_BUSY, | ||
- | MY1SBIT(LCD_DNC, | ||
- | MY1SBIT(LCD_RNW, | ||
- | MY1SBIT(LCD_ENB, | ||
- | / | ||
- | #include " | ||
- | / | ||
- | #define SERVO_PIN P1_0 | ||
- | #include " | ||
- | / | ||
- | MY1SBIT(GO000, | ||
- | MY1SBIT(GO1MS, | ||
- | MY1SBIT(GO2MS, | ||
- | / | ||
- | #define BUFFSIZE 128 | ||
- | / | ||
- | __xdata char buff[BUFFSIZE]; | ||
- | / | ||
- | void main(void) { | ||
- | unsigned char curr, test; | ||
- | GO000 = 1; GO1MS = 1; GO2MS = 1; | ||
- | timer_init(); | ||
- | servo_init(); | ||
- | lcd_init(); | ||
- | lcd_goto_line1(); | ||
- | lcd_puts(" | ||
- | uart_init(); | ||
- | uart_puts(" | ||
- | hc06_init(); | ||
- | curr = 3; | ||
- | do { | ||
- | uart_puts(" | ||
- | hc06_find(); | ||
- | if (hc06_wait_ok()==HC06_OK) { | ||
- | uart_puts(" | ||
- | uart_puts(" | ||
- | uart_puts(" | ||
- | uart_puts(BTNAME); | ||
- | uart_puts(" | ||
- | hc06_setname(buff, | ||
- | uart_puts(buff); | ||
- | uart_puts(" | ||
- | timer_delay1s(test, | ||
- | /* set pass */ | ||
- | uart_puts(" | ||
- | uart_puts(BTPASS); | ||
- | uart_puts(" | ||
- | hc06_setpin(buff, | ||
- | uart_puts(buff); | ||
- | uart_puts(" | ||
- | timer_delay1s(test, | ||
- | break; | ||
- | } | ||
- | uart_puts(" | ||
- | timer_delay1s(test, | ||
- | } while (--curr); | ||
- | while (1) { | ||
- | if (hc06_peek()) { | ||
- | if (hc06_wait(buff, | ||
- | if (buff[0]=='#' | ||
- | curr = (unsigned char) str2uint(& | ||
- | if (curr> | ||
- | uart_puts("## | ||
- | uart_puts(& | ||
- | uart_puts(" | ||
- | servo_turn(curr); | ||
- | } else { | ||
- | uart_puts(" | ||
- | uart_puts(& | ||
- | uart_puts(" | ||
- | } | ||
- | } else { | ||
- | uart_puts(">> | ||
- | uart_puts(buff); | ||
- | uart_puts(" | ||
- | } | ||
- | } | ||
- | } | ||
- | if (GO000==0) { | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | servo_turn(15); | ||
- | while (!GO000); | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | } | ||
- | if (GO1MS==0) { | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | servo_turn(10); | ||
- | while (!GO1MS); | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | } | ||
- | if (GO2MS==0) { | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | servo_turn(20); | ||
- | while (!GO2MS); | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | } | ||
- | } | ||
- | } | ||
- | / | ||
- | </ | ||
- | ++++ | ||
- | |||
- | ++++ Testing Base Kit (202425s1). | | ||
- | <file c basetest.c> | ||
- | / | ||
- | /* override default tick values */ | ||
- | #define TIMER_TICK_LEN TIMER_VAL50MS | ||
- | #define TIMER_TICK_CNT TIMER_LOOP_1S | ||
- | / | ||
- | #include " | ||
- | #include " | ||
- | #include " | ||
- | / | ||
- | void main(void) { | ||
- | unsigned char loop, mask, seg7[] = { _7SEGNUM_CC_ }; | ||
- | P1MODE1(); // better pullup current | ||
- | loop = 0; mask = (!P2_7)? | ||
- | P1 = seg7[loop] ^ mask; | ||
- | timer_init(); | ||
- | timer_tick_exec(); | ||
- | while (1) { | ||
- | P2_0 = !P3_7; | ||
- | P2_1 = !P3_6; | ||
- | P2_2 = !P3_5; | ||
- | P2_3 = !P3_4; | ||
- | if (timer_ticked()) { | ||
- | loop++; if (loop==10) loop = 0; | ||
- | mask = (!P2_7)? | ||
- | P1 = seg7[loop] ^ mask; | ||
- | timer_tick00(); | ||
- | } | ||
- | } | ||
- | } | ||
- | / | ||
- | </ | ||
- | ++++ | ||
- | |||
- | ===== Code: relay ===== | ||
- | |||
- | ++++ Testing basic relay. | | ||
- | <file c relay.c> | ||
- | / | ||
- | #include " | ||
- | #include " | ||
- | / | ||
- | MY1SBIT(DRIVE, | ||
- | MY1SBIT(INPUT, | ||
- | / | ||
- | void main(void) { | ||
- | unsigned char wait; | ||
- | timer_init(); | ||
- | uart_init(); | ||
- | uart_puts(" | ||
- | DRIVE = 1; /* active low */ | ||
- | INPUT = 1; | ||
- | while(1) { | ||
- | if (INPUT==0) { | ||
- | uart_puts(" | ||
- | DRIVE = 0; | ||
- | wait = 3*TIMER_LOOP_1S; | ||
- | do { timer_wait(TIMER_VAL50MS); | ||
- | uart_puts(" | ||
- | DRIVE = 1; | ||
- | while (!INPUT); | ||
- | } | ||
- | } | ||
- | } | ||
- | / | ||
- | </ | ||
- | ++++ | ||
- | |||
- | ===== Code: gtuc51 ===== | ||
- | |||
- | ++++ Test code for the old GTUC51B001 development board. | | ||
- | <file c gtuc51.c> | ||
- | / | ||
- | /** | ||
- | * Demo program for gtuc51 (with io board) | ||
- | **/ | ||
- | / | ||
- | #include " | ||
- | #include " | ||
- | #include " | ||
- | #include " | ||
- | / | ||
- | /** | ||
- | * Switch & LED Interface for gtuc51 i/o board | ||
- | | ||
- | | ||
- | **/ | ||
- | /** SW0, SW1 => P3.3, P3.2 - LAYOUT ERROR */ | ||
- | __sbit __at (0xB3) SW0; | ||
- | __sbit __at (0xB2) SW1; | ||
- | /** LED{0-3} => P3.4-P3.7 */ | ||
- | __sbit __at (0xB4) LED0; | ||
- | __sbit __at (0xB5) LED1; | ||
- | __sbit __at (0xB6) LED2; | ||
- | __sbit __at (0xB7) LED3; | ||
- | /** LED{RX,TX} => P3.1, P3.0 - LAYOUT ERROR */ | ||
- | __sbit __at (0xB1) LEDRX; | ||
- | __sbit __at (0xB0) LEDTX; | ||
- | /** alias LEDX=-2 and LEDY=-1 (LEFT OF LED0) **/ | ||
- | __sbit __at (0xB1) LEDX; | ||
- | __sbit __at (0xB0) LEDY; | ||
- | / | ||
- | char display[LCD_MAX_CHAR]; | ||
- | unsigned char lcdi, loop; | ||
- | float value; | ||
- | keybyte_t keyin; | ||
- | adcbyte_t check; | ||
- | __bit adc, adcgo, left, demo; | ||
- | / | ||
- | #define DEMO_IO 1 | ||
- | #define DEMO_ADC 0 | ||
- | / | ||
- | #define LOOP_COUNT 20 | ||
- | / | ||
- | /* interrupt service routine for timer0 */ | ||
- | void timer_blink(void) __interrupt TF0_VECTOR { | ||
- | TR0 = 0; loop--; | ||
- | P1 = ~P1; | ||
- | if (loop==0) { | ||
- | if (!left) { | ||
- | loop = LED0; LED0 = LED1; LED1 = LED2; LED2 = LED3; | ||
- | LED3 = LEDX; LEDX = LEDY; LEDY = loop; | ||
- | } | ||
- | else { | ||
- | loop = LED3; LED3 = LED2; LED2 = LED1; LED1 = LED0; | ||
- | LED0 = LEDY; LEDY = LEDX; LEDX = loop; | ||
- | } | ||
- | loop = LOOP_COUNT; | ||
- | } | ||
- | TH0 = 0x4B; TL0 = 0xFD; TR0 = 1; /** 50ms */ | ||
- | } | ||
- | / | ||
- | /* interrupt service routine for timer1 */ | ||
- | void timer_goadc(void) __interrupt TF1_VECTOR { | ||
- | TR1 = 0; TH1 = 0x4B; TL1 = 0xFD; | ||
- | if (loop>0) { | ||
- | TR1 = 1; /** 50ms */ | ||
- | loop--; | ||
- | } | ||
- | else { | ||
- | loop = LOOP_COUNT; | ||
- | adcgo = adc; | ||
- | } | ||
- | } | ||
- | / | ||
- | /* interrupt service routine for int1 */ | ||
- | void check_switch0(void) __interrupt IE1_VECTOR { | ||
- | left = 0; | ||
- | if (lcdi< | ||
- | lcd_data(0x30); | ||
- | lcdi++; | ||
- | } | ||
- | } | ||
- | / | ||
- | /* interrupt service routine for int0 */ | ||
- | void check_switch1(void) __interrupt IE0_VECTOR { | ||
- | left = 1; | ||
- | if (lcdi< | ||
- | lcd_data(0x31); | ||
- | lcdi++; | ||
- | } | ||
- | } | ||
- | / | ||
- | /* main function */ | ||
- | void main(void) { | ||
- | /** initalize stuffs */ | ||
- | demo = DEMO_IO; /* default... just in case */ | ||
- | TMOD = 0x11; P1 = 0xFF; P3 = 0xFF; | ||
- | lcd_init(); | ||
- | lcd_goto_line1(); | ||
- | lcd_puts(" | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | /* wait for user key press */ | ||
- | while (1) { | ||
- | if (!SW0) { | ||
- | demo = DEMO_IO; /* demo switch, led, keypad and P1 */ | ||
- | while(!SW0); | ||
- | break; | ||
- | } | ||
- | else if (!SW1) { | ||
- | demo = DEMO_ADC; /* demo adc */ | ||
- | while(!SW1); | ||
- | break; | ||
- | } | ||
- | } | ||
- | /* select! */ | ||
- | if (demo==DEMO_IO) { | ||
- | lcd_goto_line1(); | ||
- | lcd_puts(" | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | lcd_goto_line2(); | ||
- | /** set timer 0 overflow every 50ms - with interrupt handler */ | ||
- | loop = LOOP_COUNT; lcdi = 0; left = 0; | ||
- | P1 = 0xAA; LED0 = 0; IT0 = 1; IT1 = 1; | ||
- | EA = 1; ET0 = 1; EX0 = 1; EX1 = 1; | ||
- | TH0 = 0x4B; TL0 = 0xFD; TR0 = 1; | ||
- | /** main loop */ | ||
- | while (1) { | ||
- | keyin = key_wait_922(); | ||
- | if (keyin< | ||
- | lcd_data(keyin+0x30); | ||
- | lcdi++; | ||
- | } | ||
- | else if(keyin==0x0F) { /** '#' | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | lcd_goto_line2(); | ||
- | lcdi = LCD_MAX_CHAR; | ||
- | } | ||
- | else if(keyin==0x0E) { /** ' | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | lcd_goto_line2(); | ||
- | lcdi = 0; | ||
- | } | ||
- | } | ||
- | } | ||
- | else { | ||
- | lcd_goto_line1(); | ||
- | lcd_puts(" | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | /* initialize adc */ | ||
- | adc_init(); | ||
- | adc = 0; adcgo = 0; EA = 1; | ||
- | /** adc status indicator */ | ||
- | LEDRX = adc; LEDTX = !adc; | ||
- | /** main loop */ | ||
- | while (1) { | ||
- | if (!SW0) { | ||
- | while (!SW0); /** wait until the user lets go */ | ||
- | adc = !adc; | ||
- | adcgo = adc; | ||
- | if (!adcgo) { | ||
- | ET1 = 0; TR1 = 0; | ||
- | lcd_puts(" | ||
- | } | ||
- | LEDRX = adc; LEDTX = !adc; | ||
- | } | ||
- | else if(!SW1) { | ||
- | while (!SW1); /** wait until the user lets go */ | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | } | ||
- | if (adcgo) { | ||
- | adcgo = 0; | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | /** read adc */ | ||
- | check = adc_get_data(); | ||
- | value = ((float) check / 255.0) * VREF; | ||
- | lcd_goto_line2(); | ||
- | lcd_puts(" | ||
- | float2str(display, | ||
- | lcd_puts(display); | ||
- | lcd_puts(" | ||
- | int2str(display, | ||
- | lcd_puts(display); | ||
- | lcd_puts(" | ||
- | /** set timer 1 overflow every 50ms - with interrupt handler */ | ||
- | ET1 = 1; TH1 = 0x4B; TL1 = 0xFD; TR1 = 1; | ||
- | } | ||
- | } | ||
- | } | ||
- | } | ||
- | / | ||
- | </ | ||
- | ++++ | ||
notes/code8051.1729141360.txt.gz · Last modified: by azman