1/22/2009

AT89S5X easy demo

公司同事功能上的測試
需要另一個mcu做簡單的功能
剛好就用51來做

1/19/2009

AT89S5x + SS2A16 LCM

使用 SDEC 的 LMC-SS2A16
和網路上的 LMC-SSC2A16
是同樣的所以可參考同一份PDF


想做這個是以後可用一個簡易的LCD
當做Debug使用
因為沒有ICE ~_~
所以就用變通的方法
最少這樣還能簡易的除錯
還不錯用


/*
LMC - ssc2a16 user atmel 89s52

show addr 1 2 3 4 ~~~ 16
one line 00H 01H 02H 03H 10H
two line 40H 41H 42H 43H 50H

By Brian @Taiwan
*/

#include "AT89X52.H"

#define LCM_En P1_0
#define LCM_Rs P1_1
#define LCM_Rw P1_2
#define LCM_Data_port P2

void sys_init(void); //system initial
void msDelay(unsigned int MaxDelay); //del function
void LCM_Init(void); //lcm initial
//void CheckBusy(void); //lcm chech write ok?
void LCM_Com_Writer(void); //lcm write command
void LCM_Show_Data(unsigned char addr,unsigned char *sd);

unsigned int DelayCNT;
unsigned int TimeCNT;

void main(void)
{
unsigned char SData1[]="Brian @Taiwan";
unsigned char SData2[]="LCD Test OK!";
char i=0;

sys_init();
msDelay(10);

LCM_Init();
msDelay(10);

LCM_Show_Data(0x81,SData1);
LCM_Show_Data(0xC2,SData2);

while(1)
{
// code
}
}

void sys_init(void)
{
//ALL SYSTEM REG SET
IE = 0x82; //USER INTERRUPT TIME0
TMOD = 0x02; //SET TIME 0 MODE
TH0 = 256-200; //SET RELOAD VALUE
TL0 = 256-200; // FAST VALUE
TimeCNT=0;
TR0=1; //time enabled
P1=0x00;
P2=0x00;
}

void Time0_int(void)interrupt 1
{
TimeCNT++;
if(TimeCNT>=10)
{
DelayCNT++;
TimeCNT=0;
}
}

void msDelay(unsigned int MaxDelay)
{
DelayCNT = 0x0000;
while(DelayCNT < MaxDelay) {}
}

void LCM_Init(void)
{
LCM_Data_port = 0x30;
LCM_Com_Writer();

LCM_Data_port = 0x30;
LCM_Com_Writer();
/*
function set 001(dl)(n)(f)xx
(dl=1 data=8bit dl=0 4bit)
(n=1 double line n=0 one line)
(f=0=5*7dot f=1 5*10 dot)
*/
LCM_Data_port = 0x38;
LCM_Com_Writer();

LCM_Data_port = 0x06;//enter mode 01(ld)(s) ld=0 addr++ ld=1 addr-- s=1 addr shift
LCM_Com_Writer();

LCM_Data_port = 0x01;//clear lcm show data
LCM_Com_Writer();

/* lcd on/off 1(d)(c)(b) 1=lcd on 0=lcd off d=lcd c=cursor b=bline */
LCM_Data_port = 0x0C;
LCM_Com_Writer();
}


void LCM_Com_Writer(void)
{
LCM_Rs=0;
LCM_Rw=0;
LCM_En=1;
msDelay(1);
LCM_En=0;
msDelay(1);
}

void LCM_Data_Writer(void)
{
LCM_Rs=1;
LCM_Rw=0;
LCM_En=1;
msDelay(1);
LCM_En=0;
LCM_Rs=0;
msDelay(1);
}

void LCM_Show_Data(unsigned char addr,unsigned char *sd)
{
if(addr!=0)
{
LCM_Data_port = addr;
LCM_Com_Writer();
}

while(*sd!=0)
{
LCM_Data_port = *sd++;
LCM_Data_Writer();
}
}
/*void CheckBusy(void)
{
unsigned char i=0x80;

LCM_Rs=0;
LCM_Rw=1;
LCM_En=1;

while(i&0x80)
{
i=P2;
msDelay(2);
}
LCM_Rs=0;
LCM_Rw=0;
LCM_En=0;
} */


1/16/2009

AT89S5X + G_SENSOR

G_SENSOR 使用的是BOSH BMA020
通訊介面是用I2C
由於89s5x沒有提供硬體I2C介面
所以就用軟體模擬

程式只是試驗用的
所以沒有寫的很嚴謹
如果G_SENSOR有問題的話
整個程式會當在ACK
有需要的人可自已在改寫
INT腳未拉出


/*
BMA020 G_SENSOR 3.3V
需注意io 可使用在16 or 32 bit mcu
USER I2C INTERFACE

By Brian @Taiwan
*/
#include "AT89X52.H"

#define GS_SDI 0x80 //P2.7 low
#define GS_SCK 0x40 //P2.6 low

#define GS_Read 0x71
#define GS_Write 0x70

void sys_init(void);
void msDelay(unsigned int MaxDelay);

void Init_GS(void);
void Send_Comm(unsigned char tx_data);
void Receive_ACK(void);
unsigned char read_Data(unsigned char send_reg);
unsigned char read_Comm(void);
void I2C_Stop(void);
void I2C_Start(void);
void Send_ACK(void);

unsigned int DelayCNT;
unsigned int TimeCNT;

void main(void)
{
unsigned char x_axis_lsb=0,x_axis_msb=0;

sys_init();

Init_GS();

while(1)
{
x_axis_lsb=read_Data(0x02);
x_axis_msb=read_Data(0x03);
//you code
}
}

void sys_init(void)
{
//ALL SYSTEM REG SET
IE = 0x82; //USER INTERRUPT TIME0
TMOD = 0x02; //SET TIME 0 MODE
TH0 = 256-200; //SET RELOAD VALUE
TL0 = 256-200; // FAST VALUE
TimeCNT=0;
TR0=1; //time enabled
P2 = 0x00; //gs port default
}

void Time0_int(void)interrupt 1
{
TimeCNT++;
if(TimeCNT>=10)
{
DelayCNT++;
TimeCNT=0;
}
}

void msDelay(unsigned int MaxDelay)
{
DelayCNT = 0x0000;
while(DelayCNT < MaxDelay) {}
}


void Init_GS(void)
{
I2C_Start();
Send_Comm(GS_Write);
Receive_ACK();
Send_Comm(0x0A); //regedit
Receive_ACK();
Send_Comm(0x02); //soft reset
Receive_ACK();
I2C_Stop();

I2C_Start();
Send_Comm(GS_Write);
Receive_ACK();
Send_Comm(0x14);
Receive_ACK();
Send_Comm(0x16); //+/-4g 1500hz
Receive_ACK();
I2C_Stop();

I2C_Start();
Send_Comm(GS_Write);
Receive_ACK();
Send_Comm(0x15);
Receive_ACK();
Send_Comm(0x40);//enable adv_int
Receive_ACK();
I2C_Stop();

I2C_Start();
Send_Comm(GS_Write);
Receive_ACK();
Send_Comm(0x11);
Receive_ACK();
Send_Comm(0x00);//any_motion_dur hg lg set
Receive_ACK();
I2C_Stop();

I2C_Start();
Send_Comm(GS_Write);
Receive_ACK();
Send_Comm(0x0F);
Receive_ACK();
Send_Comm(0x0A); //hg_dur
Receive_ACK();
I2C_Stop();

I2C_Start();
Send_Comm(GS_Write);
Receive_ACK();
Send_Comm(0x0E);
Receive_ACK();
Send_Comm(0xE0); //hg_thres
Receive_ACK();
I2C_Stop();

I2C_Start();
Send_Comm(GS_Write);
Receive_ACK();
Send_Comm(0x0C);
Receive_ACK();
Send_Comm(0x10); //lg_thres
Receive_ACK();
I2C_Stop();

I2C_Start();
Send_Comm(GS_Write);
Receive_ACK();
Send_Comm(0x0B);
Receive_ACK();
Send_Comm(0x57); //any & hg & lg_enable hg & lg_counter
Receive_ACK();
I2C_Stop();

I2C_Start();
Send_Comm(GS_Write);
Receive_ACK();
Send_Comm(0x0A);
Receive_ACK();
Send_Comm(0x00);
Receive_ACK();
I2C_Stop();
}


void Send_Comm(unsigned char tx_data)
{
unsigned char Port_status;
unsigned char temp;
int i;

for(i=0;i<8;i++)
{
temp = tx_data & GS_SDI;
Port_status = P2;

if(temp)
Port_status |= GS_SDI;
else
Port_status &= ~GS_SDI;

P2 = Port_status;

tx_data<<=1;

// msDelay(1);
P2 |= GS_SCK;
// msDelay(1);
P2 &= ~GS_SCK;
// msDelay(1);
}
}

void Receive_ACK(void)
{
unsigned int i,temp;

P2 |= GS_SDI;
// msDelay(1);
P2 |= GS_SCK;
i=1;
while(i)
{
temp = P2;
temp &=GS_SDI;
if(temp==0)
i=0;
// msDelay(1);
}
P2 &= ~GS_SCK;
// msDelay(1);
}

void I2C_Start(void)
{
P2 |= GS_SCK;
P2 |= GS_SDI;
//msDelay(2);
P2 &= ~GS_SDI;
// msDelay(2);
P2 &= ~GS_SCK;
//msDelay(2);
}

void I2C_Stop(void)
{
P2 |= GS_SCK;
// msDelay(2);
P2 |= GS_SDI;
}

unsigned char read_Data(unsigned char send_reg)
{
unsigned char Rx_data;

I2C_Start();
Send_Comm(GS_Write);
Receive_ACK();
Send_Comm(send_reg);
Receive_ACK();
I2C_Stop();

//msDelay(1);

I2C_Start();
Send_Comm(GS_Read);
Receive_ACK();
// msDelay(1);
Rx_data = read_Comm();
Send_ACK();
I2C_Stop();

return Rx_data;
}

unsigned char read_Comm(void)
{
unsigned char Port_status;
unsigned char temp=0x00;
int i;

for(i=0;i<8;i++)
{
Port_status=P2 & GS_SDI;
Port_status>>=i;
temp|= Port_status;

// msDelay(1);
P2 |= GS_SCK;
// msDelay(1);
P2 &= ~GS_SCK;
// msDelay(1);
}
return temp;
}

void Send_ACK(void)
{
P2 &= ~GS_SDI;
// msDelay(1);
P2 |= GS_SDI;
// msDelay(1);
}

1/15/2009

電容換算

有時拿到電容時
還要細看編號
對於我們這種較笨的人
還要拿個紙筆計算一下
還會算錯才囧


因為同事的建議就寫了這個簡易換算程式
只要輸入電容編號就ok
但不能反算
因為是簡易的 XDD

這次提供了 DOS 和 WINDOWS 二種版本
之前的RGB轉換也有小改
CAP.rar
WCAP.rar

Source Code:

/*
By Brian @Taiwan
*/
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
int CAP;
int head;
int modu;
int flag=1,exit=1;
int i;
int exit_ans;
unsigned long ans;
float ansf;

printf("Wellcome user CAP program. \n");
printf("CAP Ver 1.0 \n");
printf("By brian @Taiwan. \n\n");
printf("Ex:Please input CAP number :102 \n");
printf(" 102 CAP = 1000pF \n\n");

while(exit)
{
while(flag)
{
printf("Please input CAP number :");
scanf("%d",&CAP);
if(CAP>=100)
flag=0;
else
printf("CAP number is error!! \n");
}
ans=1;
modu = CAP %10;
head = CAP /10;

if(modu<=7)
{
for(i=0;i<modu;i++)
ans *= 10;
ans *= head;
printf("%d CAP = %dpF \n",CAP,ans);
}
else
printf("%d CAP = %d*10^%dpF \n",CAP,head,modu);

if(modu<=7)
{
if(modu<=2)
{
ansf = (float)ans/1000;
printf(" = %fnF \n",ansf);
}
else
{
ans /= 1000;
printf(" = %dnF \n",ans);
}
}
else
{
modu-=3;
for(i=0;i<modu;i++)
ans *= 10;
ans *= head;
printf(" = %dnF \n",ans);
}

if(modu<=2)
ansf = (float)ansf/1000;
else
ansf = (float)ans/1000;
printf(" = %fuF \n\n",ansf);

printf("Do you want exit? 1.yes 2.no :");
scanf("%d",&exit_ans);
if(exit_ans==1)
exit=0;
else
{
exit=1;
printf("\n");
flag=1;
}
}
return 0;
}

1/13/2009

我願意

王菲
是我最欣賞的藝人
喜愛她的歌聲
特別是早期的作品
常讓我回味

天空......一張很舊的專輯
但..很好聽

王菲 我願意

思念是一種很玄的東西,如影隨形
無聲又無息出沒在心底,轉眼.....吞沒我在寂寞裡
我無力抗拒,特別是夜裡......哦~~想你到無法呼吸
恨不能立即朝你狂奔去 大聲的告訴你.......
我願意為你....忘記我姓名
就算多一秒,停留在你懷裡,失去世界也不可惜
我願意為你....被放逐天際
只要你真心,拿愛與我回應,我什麼都願意...什麼都願意......為你!

1/12/2009

思念

原來對人,對物的思念
能夠這麼的清悉

或許是第一次來到外鄉
一切總是陌生,孤獨

對人,想念那種溫暖的擁抱
對物,想念那種熟悉的環境

我是那麼想妳......