Thursday, September 18, 2008

C語言入門

C語言在單晶片設計複雜度不高,變數型態宣告沒有浮點數,常用以下兩種型態。

char ===> 範圍 0 - 255

int ===> 範圍 0 - 65535

算術運算子: + 、 - 、 * 、 / 、 %(module)

關係運算子:>、<、>=(大於等於)、<=(小於等於)、==(等於)、!=(不等於)

邏輯運算子:&& (AND Gate)、(OR Gate)、!(NOT Gate)

補數運算子:~ , 1的補數為0,其他反之。

※ ;(分號)在C語言中它表示敘述句結束。

迴圈語法:

for 迴圈

語法:

for(char x = initial number ; x <= condition number ; x increment or decrement)

ex .
for(x = 0 ; x <= 9 ; x++)
{
x += 1;(0+1+...+9)
}

ex . for(;;),無窮迴圈

while 迴圈

語法:

char x = initial number ;
while(condition number) <=====這裡不加分號
{
statement;
x increment or decrement;
}

ex.
int x = 0;
while(x < 10)
{
x += 1;(0+1+2....+9)
x++;
}

ex. while(1),無窮迴圈

do while 迴圈

語法:

char x = initial number ;
do
{
statement;
x increment or decrement;
}while(condition number); <=====這裡要加分號

ex.
int x = 0;
do
{
x += 1;(0+1+2....+9)
x++;
}while(x < 10);

Sunday, September 14, 2008

8051入門

一、AT89S51特色
1. AT89S51 為 MCS-51 相容產品。

2. 4K Byte of In - System Programmable (ISP) Flash Memory。
3. Endurance:1000 Write / Erase Cycles。
4. 工作電壓範圍為 4.0 - 5.5 伏特。
5. 可使用振盪器 0 - 33 MHz 。
6. 128 * 8 bit Internel RAM。
7. 32條可程式輸出/入線。
8. 兩個16位元計時/計數器。
9. 六個中斷來源。
10.全雙工通用非同步接收及傳送序列通道。
二、AT89S51接腳及定義:
該圖片摘錄於ATMEL AT89S51 Datasheet。



















VCC:接 5V 正電壓。
GND:接地腳。
Port 0:
1. 可作為一個 8 位元輸出 / 入腳。
2. 外部擴充接腳(例如8255晶片)時可做資料匯流排(D0 - D7)。
3. 外部擴充記憶體(例如6116晶片)時可做位址匯流排(A0 - A7)。
Port 1:只作為一個 8 位元輸出 / 入腳。
Port 2:
1. 可作為一個 8 位元輸出 / 入腳。
2. 外部擴充記憶體做位址匯流排(A8 - A15)。
Port 3:
1. 可作為一個 8 位元輸出 / 入腳。
2. 特殊功能:
● RXD (串列輸入埠 [Serial input poirt])
●TXD (串列輸出埠 [serial output point])
●INT0 (外部中斷0 [External interrupt 0])
●INT1 (外部中斷1 [External interrupt 1])
●T0 (計時/計數中斷[內部為計時;外部為計數])
●T1 (計時/計數中斷[內部為計時;外部為計數])
●WR (外部資料記憶體寫入致能信號)
●RD (外部資料記憶體讀取致能信號)
RST l:高準位時(保持2個機械週期),可重置晶片。
ALE / PROG:位址拴鎖致能(Address Latch Enable)。
PSEN:程式儲存致能(Program Store Enable)。
EA / VPP:
●高電位:
1. CPU讀取內部程式記憶體(ROM)。8051、8052
2. 有擴充外部記憶體時:當內部記憶體不敷使用時0FFFH(8051)、1FFFH(8052),自動讀取外
部記憶體。
●低電位:CPU僅讀取外部記憶體(ROM)。
●8751燒錄:利用此腳輸入21V的燒入電壓。
XTAL 1:接石英振盪器。
XTAL 2:接石英振盪器。
三、AT89S51記憶體:
8051的記憶體包括:
1、8051 有 128 Bytes RAM,8052 有 256 Bytes RAM。
2、擴充外部的 RAM,最大為 64K。
3.記憶體位址 00 ~ 7FH 較低位址共128 byte。
4.記憶體位址 80H ~ FFH 較低位址共128 byte。
5.特殊功能暫存器(Special Function Registor)。

Thursday, September 11, 2008

Make MCU Board - Thermometer (DS1821) module


The reason of choosing DS1821 thermo sensor is that the circuit is very simple, it only need on  IO pin.

Originally, I wrote the test program by SDCC compiler, the test has some problem, the LCD display is work, but the temperature is still display zero?
Then, I did compiling by HI-TECH's PICC, the temperature value is correct.
If you are using PICC, please comment out lines as following:

typedef unsigned int config;
config at 0x2007 __CONFIG = _XT_OSC & _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_OFF;

Replace pic/pic16f877a.h by htc.h

Circuit schematics is as following:












Main process:
/*******************************************************************************

 Copyright (c) 2008 Wizign Ltd.
 All Rights Reserved.

 main.c: Main program for demo thermometer

 Version: 1.0.0

 Date: Sep 05, 2008

 Author: YenHung Chen

 E-mail: yhchen@wizign.com

 Revision:
 ---------- -----------------------------------------------------------------
 2008/09/05 Created by YenHung Chen, demo thermometer for both 8051 and PIC

******************************************************************************/
/*******************************************************************************

The IO control

<>For AT89S52

LCD display:
There are 8 data pins, which are connected P2.
The control pins are connected to P1_0, P1_1, and P1_2.

DS1821:
P1_7

<>For PIC16F877A

LCD display:
There are 8 data pins, which are connected PORTD
The control pins are connected to RA0, RA1, and RA2.

DS1821:
RA5


******************************************************************************/

#include "global.h"
#include "delay.h"
#include "lcd.h"
#include "thermo.h"

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// define IO and global variables

#ifdef MCU_PIC

// Configurations
typedef unsigned int config;
config at 0x2007 __CONFIG = _XT_OSC & _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_OFF;

#endif


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// define functions

/* Initilize parameters before run the main loop */
void InitParams(void)
{

LCDInit();
ThermoInit();
}

/* Display message on LCD panel */
void DisplayTemperature(void)
{
char tmMsg[16];
int theT;
unsigned char myT;
int hundred;
int tenth;
int ones;

LCDGoto(0); // goto 1st line
LCDPuts("  WIZIGN LTD.  ");

// Convert value to string
myT = ThermoReadTemperature();
if(myT > 127){
   theT = (int)(myT - 0xFF);
}else{
   theT = (int)myT;
}
// start
// it can not display correct format in this block
BLOCK
// end
   hundred = (int)(theT/100);
   tenth = (int)((theT%100)/10);
   ones = theT%10;
   tmMsg[1] = 0x30 + hundred;
   tmMsg[2] = 0x30 + tenth;
   tmMsg[3] = 0x30 + ones;
   tmMsg[4] = 0x20;
   tmMsg[5] = 'C';
   tmMsg[6] = 0x20;
   tmMsg[7] = '\0';

   LCDGoto(0x40); // goto 2nd line
   LCDPuts(tmMsg);
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// main program

void main(void)
{
   // Initilize parameters
 InitParams();

   /* do loop */
   while(1){
    DisplayTemperature();
       // The LCD display and the calculation of time
       // will consume times, so, the delay time is less than 1000 ms.
       DelayMs(900);
   }
}

Wednesday, September 3, 2008

Make MCU Board - EDA Tool - KiCAD


When we design functional MCU module, the circuit schematics is necessarily step for checking IO port.

Except OrCAD and Protel, there are some open source EDA tools.

KiCAD is an open source (GPL) software, we can use KiCAD to design circuit schematics and layout PCB, the author of KiCAD is Jean-Pierre Charras( a research of LIS,Laboratoire des Images et des Signaux, in Frence, he is a electronics and image processing teacher in IUT de Saint Martin d'Hères), he design and coding the KiCAD software.

KiCAD is a composite tools, it includes a project management and 4 specially functional software:

* Eeschema : Design circuit schematics
* Pcbnew : Design PCB
* Gerbview : GERBER viewer
* Cvpcb : For selecting electronic components, while designing circuit.
* Kicad:Project management

KiCAD can use Wings 3D to simulate 3D PCB.

KiCAD does not only run on Windows OS, it can run on Linux, too.

For Fedora Core 9:

# yum install kicad

For Ubuntu 8.04

# apt-get install kicad


The GUI of KiCAD is as following:

Kicad












eeschema












cvpcb











pcbnew












3D simulation












The functions of KiCAD is complete, try it!
If you are software developer, you can download the source code from the official site.

Tuesday, September 2, 2008

Make MCU Board - Digital Clock (LCD Display) module

To calculate time by using delay method, it will causes obvious time deviation, and display the digital value on a LCD panel.

The LCD type is PVC160203PGL01.

The MCU is PIC 16F877A, define IO as following:

RA2 LCD_EN
RA1 LCD_RW
RA0 LCD_RS
PORTD LCD_DATA

If MCU uses AT89S51/52/53, IO definition is:

P1_2 LCD_EN
P1_1 LCD_RW
P1_0 LCD_RS
P2 LCD_DATA

The circuit schematic:



Main program is as following:

/*******************************************************************************

 Copyright (c) 2008 Wizign Ltd.
 All Rights Reserved.

 main.c: Main program for demo LCD display

 Date: Aug 29, 2008

 Author: YenHung Chen

 E-mail: yhchen@wizign.com

 Revision:
 ---------- -----------------------------------------------------------------
 2008/08/29 Created by YenHung Chen, demo LCD display for both 8051 and PIC

******************************************************************************/
/*******************************************************************************



<>For AT89S52

There are 8 data pins, which are connected P2.
The control pins are connected to P1_0, P1_1, and P1_2.


<>For PIC16F877A

There are 8 data pins, which are connected PORTD
The control pins are connected to RA0, RA1, and RA2.


******************************************************************************/

#include "global.h"
#include "delay.h"
#include "lcd.h"

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// define IO and global variables

#ifdef MCU_PIC

// Configurations
typedef unsigned int config;
config at 0x2007 __CONFIG = _XT_OSC & _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_OFF;

#endif

int gTmYr;
unsigned char gTmMon;
unsigned char gTmDay;
unsigned char gTmHr;
unsigned char gTmMin;
unsigned char gTmSec;


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// define functions

/* Initialize the begin time */
void InitCurrentTime(int y, unsigned char m, unsigned char d,
                  unsigned char hr, unsigned char min, unsigned char sec){
 gTmYr = y;
 gTmMon = m;
 gTmDay = d;
 gTmHr = hr;
 gTmMin = min;
 gTmSec = sec;
}

/* Calculate the progressing time */
void CalculateTime(void)
{
 gTmSec++;
 if(gTmSec >= 60){
     gTmSec = 0;
     gTmMin++;
     if(gTmMin >= 60){
         gTmMin = 0;
         gTmHr++;
         if(gTmHr >= 24){
             gTmHr = 0;
             gTmDay++;
             switch(gTmMon){
             case 2:
                 if(((gTmYr%4)==0)&&((gTmYr%100)!=0)&&((gTmYr%400)==0)){
                     if(gTmDay >= 29){
                         gTmDay = 1;
                         gTmMon++;
                     }
                 }else{
                     if(gTmDay >= 28){
                         gTmDay = 1;
                         gTmMon++;
                     }
                 }
                 break;
             case 4:
             case 6:
             case 9:
             case 11:
                 if(gTmDay >= 30){
                     gTmDay = 1;
                     gTmMon++;
                 }
                 break;
             default:    // 31 days/month
                 if(gTmDay >= 31){
                     gTmDay = 1;
                     gTmMon++;
                     if(gTmMon >= 12){
                         gTmMon = 1;
                         gTmYr++;
                     }
                 }
                 break;
             }
         }
     }
 }
}

/* Initilize parameters before run the main loop */
void InitParams(void)
{
 lcd_init();
 InitCurrentTime(2008, 9, 1, 10, 24, 12);
}

/* Power value of 10 */
int PowerTen(int p)
{
 int ret = 1;
 while(--p > 0){
     ret *= 10;
 }
 return ret;
}

/* Convert value to string */
void NumToStr(char* pMsg, int v, int len)
{
 int i;
 int p;
 for(i=0; i
     p = PowerTen(len-i);
     pMsg[i] = 0x30 + (int)(v/p);
     v %= p;
 }
}

/* Convert time value to string message */
void GetTimeMsg(char *pMsg)
{
 NumToStr(&pMsg[0], gTmMon, 2);
 pMsg[2] = '/';
 NumToStr(&pMsg[3], gTmDay, 2);
 pMsg[5] = ' ';
 NumToStr(&pMsg[6], gTmHr, 2);
 pMsg[8] = ':';
 NumToStr(&pMsg[9], gTmMin, 2);
 pMsg[11] = ':';
 NumToStr(&pMsg[12], gTmSec, 2);
 pMsg[14] = 0x00;
}

/* Display message on LCD panel */
void DisplayMessage(void)
{
 char tmMsg[16];

 lcd_goto(0);    // goto 1st line
 lcd_puts("  WIZIGN LTD.  ");

 // Convert value to string
 GetTimeMsg(tmMsg);
 lcd_goto(0x40);    // goto 2nd line
 lcd_puts(tmMsg);
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// main program

void main(void)
{
 // Initilize parameters
 InitParams();

 /* do loop */
 while(1){
     CalculateTime();
     DisplayMessage();
     // The LCD display and the calculation of time
     // will consume times, so, the delay time is less than 1000 ms.
     DelayMs(900);
 }
}