User Tools

Site Tools


archive:nmk322

NMK32203 - Microcontroller

Note20230217 This page is being updated - work in progress…

This course is Microcontroller, offered by the Faculty of Electronics Engineering & Technology.

Announcements

[20230217] Welcome to the new NMK322 info page!

Lecture Slides

Codes (discussed in class)

Note: I had to rename functions with the word p-r-i-n-t to 'puts'… somehow, UniMAP server does not like that word being sent to the server :-O

work in progress…

Library: Serial (UART)

uart.h
#ifndef __MY1UART_H__
#define __MY1UART_H__
 
void uart_init(void) {
	SCON = 0x50;
	TMOD &= 0x0F;
	TMOD |= 0x20;
	TH1 = 253;
	TR1 = 1;
}
 
void uart_tx(unsigned char sdat) {
	SBUF = sdat;
	while (TI==0);
	TI = 0;
}
 
unsigned char uart_rx(void) {
	unsigned char rdat;
	while (RI==0);
	rdat = SBUF;
	RI = 0;
	return rdat;
}
 
void uart_puts(char* text) {
	while (*text) {
		uart_tx(*text);
		text++;
	}
}
 
#endif /* __MY1UART_H__ */

Library: Text LCD

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); /* 8 bit mode, 1/16 duty, 5x8 font */
	tlcd_cmd(0x38);
	tlcd_cmd(0x38);
	tlcd_cmd(0x06); /* display off */
	tlcd_cmd(0x0c); /* display on, blink cursor on */
	tlcd_cmd(0x01); /* clear display */
}
 
void tlcd_puts(char* text) {
	while (*text) {
		tlcd_write(*text);
		text++;
	}
}
#endif /* __MY1TLCD_H__ */

Library: Timer

timer.h
#ifndef __MY1TIMER_H__
#define __MY1TIMER_H__
 
// useful macro!
#define timer0_init() { TMOD &= 0xF0; TMOD |= 0x01; }
#define timer0_prep(hi,lo) { TH0 = hi; TL0 = lo; }
#define timer0_stop() { TR0 = 0; TF0 = 0; }
#define timer0_exec() { TR0 = 1; }
#define timer0_flag() { timer0_exec(); while (!TF0); }
#define timer0_wait(hi,lo) { timer0_prep(hi,lo); timer0_flag(); timer0_stop(); }
#define timer0_null() { TH0 = 0; TL0 = 0; }
#define timer0_read() ((unsigned int)TH0<<8)|TL0
 
void timer0_delayms(unsigned int step) {
	do {
		timer0_wait(0xfc,0x66); // 1 ms delay
	} while (--step);
}
 
#endif /* __MY1TIMER_H__ */

Codes (for Lab Project)

You can get my personal 8051 code library from here (LICENSE), which can be used for NMK322 lab project. The codes provided in this repository is NOT ALLOWED to be used in any written (or lab) assessment.

archive/nmk322.txt · Last modified: 2024/02/27 21:32 by azman