/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2015 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
//***********************************************************************************************************
// Nuvoton Technoledge Corp.
// Website: http://www.nuvoton.com
// E-Mail : MicroC-8bit@nuvoton.com
// Date : Sep/1/2015
//***********************************************************************************************************
//***********************************************************************************************************
// File Function: N76E616 LCD Display Temperature Demo Code
//***********************************************************************************************************
#include <stdio.h>
#include <N76E616.h>
#include "Version.h"
#include "Typedef.h"
#include "Define.h"
#include "SFR_Macro.h"
#include "Common.h"
#include "Delay.h"
#include "Display.h"
#define SCL P06
#define SDA P05
#define TMP421_Addr 0x98
#define LT_PTR 0x00
#define STATUS_PTR 0x08
#define CR1_PTR 0x09
#define CR2_PTR 0x0A
#define ONE_SHOT_PTR 0x0F
#define SWRST_PTR 0xFC
#define MID_PTR 0xFE
#define DID_PTR 0xFF
#define TMP421_Addr 0x98
//Global variables
volatile unsigned char LED_phase = 0;
volatile float temperature;
volatile bit I2C_error = 0;
volatile bit TMP421_error = 0;
volatile unsigned char temp = 0;
bit bdata I2C_Ack; //??????
/*
//-------- <<< Use Configuration Wizard in Context Menu >>> ------------------
//
//<e0> System Clock Source Configuration
// <o1> System Clock Source Selection
// <0=> 2~16 MHz XTAL (HXT)
// <1=> 32.768 kHz XTAL (LXT)
// <2=> 11.059 MHz Internal (HIRC)
//</e>
//
//<e2> Clock Divider Configuration
// <o3.0..7> System Clock Source Devider <1-255:1>
// <i> Fsys = (System Clock Source) / (2 * Devider)
//</e>
//
// <o4> Port0 Mode Selection (P0[7:0])
// <0=> Quasi-Bidirectional
// <1=> Push-Pull
// <2=> Input-Only (high-impedance)
// <3=> Open-Drain
//
// <o5> Port1 Mode Selection (P1[7:0])
// <0=> Quasi-Bidirectional
// <1=> Push-Pull
// <2=> Input-Only (high-impedance)
// <3=> Open-Drain
//
// <o6> Port2 Mode Selection (P2[7:0])
// <0=> Quasi-Bidirectional
// <1=> Push-Pull
// <2=> Input-Only (high-impedance)
// <3=> Open-Drain
//
// <o7> Port3 Mode Selection (P3[5:0])
// <0=> Quasi-Bidirectional
// <1=> Push-Pull
// <2=> Input-Only (high-impedance)
// <3=> Open-Drain
//
// <o8> Port4 Mode Selection (P4[6:0])
// <0=> Quasi-Bidirectional
// <1=> Push-Pull
// <2=> Input-Only (high-impedance)
// <3=> Open-Drain
//
// <o9> Port5 Mode Selection (P5[7:0])
// <0=> Quasi-Bidirectional
// <1=> Push-Pull
// <2=> Input-Only (high-impedance)
// <3=> Open-Drain
//-------- <<< end of configuration section >>> ------------------------------
*/
#include <absacc.h>
#define SYS_CLK_EN 1
#define SYS_SEL 2
#define SYS_DIV_EN 0 //0: Fsys=Fosc, 1: Fsys = Fosc/(2*CKDIV)
#define SYS_DIV 1
#define PORT0_MODE 2
#define PORT1_MODE 2
#define PORT2_MODE 2
#define PORT3_MODE 2
#define PORT4_MODE 2
#define PORT5_MODE 2
bit BIT_TMP;
//------------------------------------------------
void Init_I2C(void)
{
P0M1 |= 0x60; //Configure SDA(P05) and SCL(P06) pins open-drain
P0M2 |= 0x60;
}
//------------------------------------------------
void I2C_Start()
{
SDA = 1;
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA = 0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL = 0;
_nop_();
_nop_();
}
//------------------------------------------------
void I2C_Stop()
{
SDA = 0;
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SDA = 1;
_nop_();
_nop_();
_nop_();
_nop_();
}
//------------------------------------------------
bit I2C_CheckAck(void)
{
UINT8 errtime = 255;
SDA = 1;
_nop_();
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
while(SDA)
{
errtime--;
if(errtime==0)
{
I2C_Stop();
return(0);
}
}
SCL = 0;
_nop_();
return(1);
}
//------------------------------------------------
void I2C_SendB(UINT8 c)
{
UINT8 BitCnt;
for (BitCnt=0; BitCnt<8; BitCnt++)
{
if((c<<BitCnt)&0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL = 0;
}
_nop_();
_nop_();
I2C_Ack = I2C_CheckAck();
_nop_();
_nop_();
}
//------------------------------------------------
UINT8 I2C_RcvB()
{
UINT8 retc;
UINT8 BitCnt;
retc = 0;
SDA = 1;
for(BitCnt=0;BitCnt<8;BitCnt++)
{
_nop_();
SCL = 0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
retc = retc<<1;
if(SDA==1)
{
retc = retc + 1;
}
_nop_();
_nop_();
}
SCL = 0;
_nop_();
_nop_();
return(retc);
}
//------------------------------------------------
void I2C_Ackn(bit a)
{
if(a==0)
{
SDA = 0;
}
else
{
SDA = 1;
}
_nop_();
_nop_();
_nop_();
SCL = 1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
SCL = 0;
_nop_();
_nop_();
}
//------------------------------------------------
unsigned char TMP421_reg_write(unsigned char u8AddrByte, unsigned char u8PtrRegByte, unsigned char u8DataByte)
{
I2C_Start();
I2C_SendB(u8AddrByte);
if(!I2C_Ack)
{
return(0);
}
I2C_SendB(u8PtrRegByte);
if(!I2C_Ack)
{
return(0);
}
I2C_SendB(u8DataByte);
if(!I2C_Ack)
{
return(0);
}
I2C_Stop();
return(1);
}
//------------------------------------------------
unsigned char TMP421_reg_read(unsigned char u8AddrByte, unsigned char u8PtrRegByte)
{
unsigned char u8RtnValue=0;
I2C_Start();
I2C_SendB(u8AddrByte);
if(!I2C_Ack)
{
return(0);
}
I2C_SendB(u8PtrRegByte);
if(!I2C_Ack)
{
return(0);
}
I2C_Start();
I2C_SendB(u8AddrByte+1);
if(!I2C_Ack)
{
return(0);
}
u8RtnValue = I2C_RcvB();
I2C_Ackn(1);
I2C_Stop();
return(u8RtnValue);
}
//------------------------------------------------
unsigned char Read_Temperature(unsigned char u8AddrByte, unsigned char u8PtrRegByte)
{
unsigned char u8ReadHiByte, u8ReadLoByte;
TMP421_reg_write(TMP421_Addr, ONE_SHOT_PTR, 0xFF); //One-shot conversion
do
{
I2C_error = 0;
I2C_Start();
I2C_SendB(u8AddrByte);
if(!I2C_Ack)
{
return(0);
}
I2C_SendB(u8PtrRegByte);
if(!I2C_Ack)
{
return(0);
}
I2C_Start();
I2C_SendB(u8AddrByte+1);
if(!I2C_Ack)
{
return(0);
}
u8ReadHiByte = I2C_RcvB();
I2C_Ackn(0);
u8ReadLoByte = I2C_RcvB()>>4;
// I2C_Ackn(1);
I2C_Stop();
//Calculate temperature degree
temperature = (float)((signed int)(u8ReadHiByte - 64) + 0.0625 * u8ReadLoByte);
}while(I2C_error);
return(1);
}
//------------------------------------------------
void Init_TMP421(void)
{
unsigned char MID, DID, i, status;
#if 1
MID = TMP421_reg_read(TMP421_Addr, MID_PTR);
if(MID != 0x55)
{
P02=0;
while(1);
}
else
P03=0;
DID = TMP421_reg_read(TMP421_Addr, DID_PTR);
if(DID != 0x21)
{
P02=0;
while(1);
}
else
P03=0;
#endif
TMP421_reg_write(TMP421_Addr, SWRST_PTR, 0xFF); //Software reset
TMP421_reg_write(TMP421_Addr, CR1_PTR, 0x44); //Change out code as extended binary, enter shutdown mode
TMP421_reg_write(TMP421_Addr, CR2_PTR, 0x08); //Enable local sensor, disable others
for (i = 1; i <= 2; i++) //Ignore first two conversions. They are not correct.
{
TMP421_reg_write(TMP421_Addr, ONE_SHOT_PTR, 0xFF); //One-shot conversion
do //Polling BUSY
{
status = TMP421_reg_read(TMP421_Addr, STATUS_PTR);
} while (status != 0x00);
}
}
//------------------------------------------------
/*------------------------------------------------
The main C function. Program execution starts
here after stack initialization.
------------------------------------------------*/
void main (void)
{
/* Note
MCU power on system clock is HIRC (11.0592MHz), so Fsys = 11.0592MHz
*/
signed int tmp;
#if 0
Set_All_GPIO_Quasi_Mode();
#if DEBUG_PORT == 0
InitialUART0_Timer1_Type1(9600); /* 9600 Baud Rate*/
#elif DEBUG_PORT == 1
InitialUART1_Timer3(9600); /* 9600 Baud Rate*/
#endif
Show_FW_Version_Number_To_PC();
printf ("\n*===================================================================");
printf ("\n* Name: N76E616 LCD Display Temperature Demo Code.");
printf ("\n*===================================================================\n");
#endif
/* Change system closk source */
#if SYS_CLK_EN == 1
#if SYS_SEL == 0
System_Clock_Select(E_HXTEN); //Fosc = 2~16MHz XTAL
#elif SYS_SEL == 1
System_Clock_Select(E_LXTEN); //Fosc = 32.768KHz XTAL
#elif SYS_SEL == 2
System_Clock_Select(E_HIRCEN); //Fosc = 11.0592MHz Internal RC
#endif
#endif
// set_CLOEN; //Clock out on P0.7
#if SYS_DIV_EN == 1
CKDIV = SYS_DIV; //Fsys = Fosc / (2* CLKDIV) = Fcpu
#endif
#if 0
Set_All_GPIO_Quasi_Mode();
#else
#if PORT0_MODE == 0 //Quasi-Bidirectional
P0M1 = 0x00;
P0M2 = 0x00;
#elif PORT0_MODE == 1 //Push-Pull
P0M1 = 0x00;
P0M2 = 0xFF;
#elif PORT0_MODE == 2 //Input-Only
P0M1 = 0xFF;
P0M2 = 0x00;
#elif PORT0_MODE == 3 //Open-Drain
P0M1 = 0xFF;
P0M2 = 0xFF;
#endif
#if PORT1_MODE == 0 //Quasi-Bidirectional
P1M1 = 0x00;
P1M2 = 0x00;
#elif PORT1_MODE == 1 //Push-Pull
P1M1 = 0x00;
P1M2 = 0xFF;
#elif PORT1_MODE == 2 //Input-Only
P1M1 = 0xFF;
P1M2 = 0x00;
#elif PORT1_MODE == 3 //Open-Drain
P1M1 = 0xFF;
P1M2 = 0xFF;
#endif
#if PORT2_MODE == 0 //Quasi-Bidirectional
P2M1 = 0x00;
P2M2 = 0x00;
#elif PORT2_MODE == 1 //Push-Pull
P2M1 = 0x00;
P2M2 = 0xFF;
#elif PORT2_MODE == 2 //Input-Only
P2M1 = 0xFF;
P2M2 = 0x00;
#elif PORT2_MODE == 3 //Open-Drain
P2M1 = 0xFF;
P2M2 = 0xFF;
#endif
#if PORT3_MODE == 0 //Quasi-Bidirectional
P3M1 = 0x00;
P3M2 = 0x00;
#elif PORT3_MODE == 1 //Push-Pull
P3M1 = 0x00;
P3M2 = 0x7F;
#elif PORT3_MODE == 2 //Input-Only
P3M1 = 0x7F;
P3M2 = 0x00;
#elif PORT3_MODE == 3 //Open-Drain
P3M1 = 0x7F;
P3M2 = 0x7F;
#endif
#if PORT4_MODE == 0 //Quasi-Bidirectional
P4M1 = 0x00;
P4M2 = 0x00;
#elif PORT4_MODE == 1 //Push-Pull
P4M1 = 0x00;
P4M2 = 0x7F;
#elif PORT4_MODE == 2 //Input-Only
P4M1 = 0x7F;
P4M2 = 0x00;
#elif PORT4_MODE == 3 //Open-Drain
P4M1 = 0x7F;
P4M2 = 0x7F;
#endif
#if PORT5_MODE == 0 //Quasi-Bidirectional
P5M1 = 0x00;
P5M2 = 0x00;
#elif PORT5_MODE == 1 //Push-Pull
P5M1 = 0x00;
P5M2 = 0xFF;
#elif PORT5_MODE == 2 //Input-Only
P5M1 = 0xFF;
P5M2 = 0x00;
#elif PORT5_MODE == 3 //Open-Drain
P5M1 = 0xFF;
P5M2 = 0xFF;
#endif
#endif
LCD_Initial();
LCD_Try_Run();
LCD_Display_All_Clear();
LCD_Display_N76E616_Pattern();
LCD_Display_Welcome_Pattern();
Timer0_Delay1ms(1000);
Init_I2C(); //Initialize I2C
Init_TMP421(); //Initialize temperature sensor by I2C commands
while(1)
{
Read_Temperature(TMP421_Addr, LT_PTR);
tmp = (signed int)(temperature*10);
LCD_Display_All_Clear();
LCD_Display_Num_2(tmp/100);
LCD_Display_Num_3(tmp%100/10);
LCD_Display_Num_4(tmp%100%10);
LCD_Display_3DP();
LCD_Display_Degree();
LCD_Display_nuvoTon_**();
LCD_Display_Welcome_Pattern();
Timer0_Delay1ms(500);
}
}
|