47. 47
/* ----- Macros ------*/
// ---- Debounce ---- //
#define HAL_DEBOUNCE(expr) { int i; for (i=0; i<500; i++) { if (!(expr)) i = 0; } }
// ---- Push Buttons ---- //
#define HAL_PUSH_BUTTON1() (PUSH1_POLARITY (PUSH1_SBIT))
#define HAL_PUSH_BUTTON2() (PUSH2_POLARITY (PUSH2_SBIT))
#define HAL_PUSH_BUTTON3() (PUSH3_POLARITY (PUSH3_SBIT))
#define HAL_PUSH_BUTTON4() (PUSH4_POLARITY (PUSH4_SBIT))
/* -------- Driver Configuration -------- */
/* Set to TRUE enable LCD usage, FALSE disable it */
#ifndef HAL_LCD
#define HAL_LCD TRUE
#endif
/* Set to TRUE enable KEY usage, FALSE disable it */
#ifndef HAL_KEY
#define HAL_KEY TRUE
#endif
#endif
/**********************************************************************************
*/
48. 加入按鍵的驅動程式
48
/**********************************************************************
Filename: hal_key.h
Revised: $Date: 2013-10-19 17:51 $
Revision: $Revision: $
Description: This file contains the interface to the KEY Service.
***********************************************************************/
#ifndef HAL_KEY_H
#define HAL_KEY_H
/****** INCLUDES ******/
#include "hal_board.h"
/****** CONSTANTS *****/
/* Interrupt option - Enable or disable */
#define HAL_KEY_INTERRUPT_DISABLE 0x00
#define HAL_KEY_INTERRUPT_ENABLE 0x01
/* Switches (keys) */
#define HAL_KEY_SW_1 0x01 // Button 1 (INT0)
#define HAL_KEY_SW_2 0x02 // Button 2 (INT1)
#define HAL_KEY_SW_3 0x04 // Button 3 (T0)
#define HAL_KEY_SW_4 0x08 // Button 4 (T1)
/******* GLOBAL VARIABLES *******/
extern bool Hal_KeyIntEnable;
/******** FUNCTIONS – API *********/
/* Initialize the Key Service */
extern void HalKeyInit(void);
/* Configure the Key Service */
extern void HalKeyConfig(bool interruptEnable);
/* Read the Key status */
extern uint8 HalKeyRead(void);
/**********************************
*************************************/
#endif
D:MySimpSystemhalinclude
49. 49
/*********************************************
Filename: hal_key.c
Revised: $Date: 2013-10-19 22:27 $
Revision: $Revision: $
Description: HAL KEY Service.
**********************************************/
/******* INCLUDES **********/
#include "hal_mcu.h"
#include "hal_defs.h"
#include "hal_types.h"
#include "hal_board.h"
#include "hal_drivers.h"
#include "hal_key.h"
#if (defined HAL_KEY) && (HAL_KEY == TRUE)
/******* CONSTANTS *******/
#define HAL_KEY_NO_EDGE 0
#define HAL_KEY_FALLING_EDGE 1
#define HAL_KEY_DEBOUNCE_VALUE 25
/* CPU port interrupt edge control and flag*/
#define HAL_KEY_CPU_TCON TCON
/* CPU port interrupt enable/disable*/
#define HAL_KEY_CPU_IE IE
D:MySimpSystemhaltargetJC51B
/********* Key 1 ************/
/* SW_1 is at P3.2 */
#define HAL_KEY_SW_1_PORT PUSH1_SBIT
/* edge interrupt */
#define HAL_KEY_SW_1_EDGEBIT BV(0) // TCON => Bit0
#define HAL_KEY_SW_1_EDGE HAL_KEY_FALLING_EDGE
/* SW_1 interrupts, INT0 IE bit and flag */
#define HAL_KEY_SW_1_IEBIT BV(0)
#define HAL_KEY_SW_1_IFG TCON_bit.IE0
/********* Key 2 ************/
/* SW_2 is at P3.3 */
#define HAL_KEY_SW_2_PORT PUSH2_SBIT
/* edge interrupt */
#define HAL_KEY_SW_2_EDGEBIT BV(2) // TCON => Bit2
#define HAL_KEY_SW_2_EDGE HAL_KEY_FALLING_EDGE
/* SW_2 interrupts, INT1 IE bit and flag */
#define HAL_KEY_SW_2_IEBIT BV(2)
#define HAL_KEY_SW_2_IFG TCON_bit.IE1
/********* Key 3 ************/
/* SW_3 is at P3.4, Key3 has no interrupt */
#define HAL_KEY_SW_3_PORT PUSH3_SBIT
/********* Key 4 ************/
/* SW_4 is at P3.5, Key4 has no interrupt */
#define HAL_KEY_SW_4_PORT PUSH4_SBIT
50. /******** GLOBAL VARIABLES ********/
static uint8 HalKeyConfigured;
/* interrupt enable/disable flag */
bool Hal_KeyIntEnable;
/******* FUNCTIONS – API ************/
/*****************************************
* @fn HalKeyInit
* @brief Initialize Key Service
* @param none
* @return None
*****************************************/
void HalKeyInit( void )
{
HAL_KEY_SW_1_PORT = 1; // make it as an input
HAL_KEY_SW_2_PORT = 1; // make it as an input
HAL_KEY_SW_3_PORT = 1; // make it as an input
HAL_KEY_SW_4_PORT = 1; // make it as an input
/* Start with key is not configured */
HalKeyConfigured = FALSE;
}
void HalKeyConfig (bool interruptEnable)
{
/* Enable/Disable Interrupt */
Hal_KeyIntEnable = interruptEnable; // Hal_KeyIntEnable is a global var.
/* Determine if interrupt is enable or not!
Interrupt is only for Key1 and Key2 */
if (Hal_KeyIntEnable)
{
/********* Key 1 ************/
/* Rising/Falling edge configuration */
HAL_KEY_CPU_TCON &= ~(HAL_KEY_SW_1_EDGEBIT);
/* For falling edge, the bit must be set. */
#if (HAL_KEY_SW_1_EDGE == HAL_KEY_FALLING_EDGE)
HAL_KEY_CPU_TCON |= HAL_KEY_SW_1_EDGEBIT;
#endif
/* Interrupt configuration: */
HAL_KEY_CPU_IE |= HAL_KEY_SW_1_IEBIT;
HAL_KEY_SW_1_IFG = 0;
/********* Key 2 ************/
/* Rising/Falling edge configuration */
HAL_KEY_CPU_TCON &= ~(HAL_KEY_SW_2_EDGEBIT);
/* For falling edge, the bit must be set. */
#if (HAL_KEY_SW_2_EDGE == HAL_KEY_FALLING_EDGE)
HAL_KEY_CPU_TCON |= HAL_KEY_SW_2_EDGEBIT;
#endif
/* Interrupt configuration: */
HAL_KEY_CPU_IE |= HAL_KEY_SW_2_IEBIT;
HAL_KEY_SW_2_IFG = 0;
/********* Key 3 ************/
/* Key3 has no interrupt */
/********* Key 4 ************/
/* Key4 has no interrupt */
50
51. } else { /* Interrupts NOT enabled */
HAL_KEY_CPU_IE &= ~(HAL_KEY_SW_1_IEBIT);
HAL_KEY_SW_1_IFG = 0;
HAL_KEY_CPU_IE &= ~(HAL_KEY_SW_2_IEBIT);
HAL_KEY_SW_2_IFG = 0;
}
/* Key now is configured */
HalKeyConfigured = TRUE;
}
/*********************************************
* @fn HalKeyRead
* @brief Read the current value of a key
* @param None
* @return keys - current keys status
********************************************/
uint8 HalKeyRead ( void )
{
uint8 keys = 0;
if (HAL_PUSH_BUTTON1())
{
HAL_DEBOUNCE(!HAL_PUSH_BUTTON1());
keys |= HAL_KEY_SW_1;
}
if (HAL_PUSH_BUTTON2())
{
HAL_DEBOUNCE(!HAL_PUSH_BUTTON2());
keys |= HAL_KEY_SW_2;
}
if (HAL_PUSH_BUTTON3())
{
HAL_DEBOUNCE(!HAL_PUSH_BUTTON3());
keys |= HAL_KEY_SW_3;
}
/****** INTERRUPT SERVICE ROUTINE *******/
/****************************************
* @fn halKeySW1Isr
* @brief SW1 ISR
* @param
* @return
***************************************/
HAL_ISR_FUNCTION( halKeySW1Isr, extern0 )
{
HAL_ENTER_ISR();
//Clear the CPU interrupt flag
HAL_KEY_SW_1_IFG = 0;
HAL_EXIT_ISR();
}
/****************************************
* @fn halKeySW2Isr
* @brief SW2 ISR
* @param
* @return
***************************************/
HAL_ISR_FUNCTION( halKeySW2Isr, extern1 )
{
HAL_ENTER_ISR();
//Clear the CPU interrupt flag
HAL_KEY_SW_2_IFG = 0;
HAL_EXIT_ISR();
}
51
63. 練習4: 加入3-wire通訊介面Driver
63
/*************************************************************************
Filename: hal_triwire.h
Revised: $Date: 2013-10-18 $
Revision: $Revision: $
Description: This file contains the interface to the 3-Wire Service.
*************************************************************************/
#ifndef HAL_TRIWIRE_H
#define HAL_TRIWIRE_H
/**** INCLUDES *****/
#include "hal_board.h"
/**** CONSTANTS ****/
/* 3-Wire delay */
#define TriWireDELAY 10
/**** FUNCTIONS – API ****/
/* 3-wire interface: write a byte to the bus */
extern void TriWire_WriteByte(uint8 output_data);
/* 3-wire interface: read a byte from the bus */
extern uint8 TriWire_ReadByte(void);
/* 3-wire interface: write data to the device register */
extern void TriWire_Write(uint8 Reg_Addr, uint8 Reg_Data)
/* 3-wire interface: read data from the device register */
extern uint8 TriWire_Read(uint8 Reg_Addr)
/************************************************************************
************************************************************************/
#endif
64. 加入3-wire的driver: hal_triwire.c
64
/**********************************************************************
Filename: hal_triwire.c
Revised: $Date: 2013-10-18 21:07 $
Revision: $Revision: $
Description: This file contains the interface to the 3-wire driver.
**********************************************************************/
/********* INCLUDES ***********/
#include "hal_board_cfg.h"
#include "hal_defs.h"
#include "hal_types.h“
#include "hal_drivers.h"
#include "hal_triwire.h"
/********* LOCAL FUNCTIONS *******/
/* 3-wire interface: write a byte to the bus */
//void TriWire_WriteByte(uint8 output_data);
/* 3-wire interface: read a byte from the bus */
//uint8 TriWire_ReadByte(void);
65. /*********************************************************
* @fn TriWire_WriteByte
* @brief Write 1-byte data to the bus
* @param output_data: The data byte to write on the bus
* @return None
*********************************************************/
void TriWire_WriteByte(uint8 output_data)
{
uint8 index;
for(index = 0; index < 8; index++) {
TriWire_SCLK = LOW;
if(output_data & 0x01) // Check LSB bit, LSB goes first
TriWire_IO = HIGH;
else
TriWire_IO = LOW;
delay_time(TriWireDELAY);
TriWire_SCLK = HIGH; // Write at this transition
delay_time(TriWireDELAY);
output_data = output_data >> 1;
}
}
/**************************************************
* @fn TriWire_ReadByte
* @brief Read 1-byte data from the bus
* @param None
* @return The read-in data byte
***************************************************/
uint8 TriWire_ReadByte(void)
{
uint8 index;
uint8 input_data = 0x00;
for(index = 0; index < 8; index++) {
input_data = input_data >> 1;
TriWire_SCLK = LOW; // Read at this transition
delay_time(TriWireDELAY);
if(TriWire_IO) // Check LSB bit, LSB comes first
input_data |= 0x80;
else
input_data |= 0x00;
TriWire_SCLK = HIGH;
delay_time(TriWireDELAY);
}
return(input_data);
}
65
66. 66
/*****************************************************************
* @fn TriWire_Write
* @brief Write data to the register on a 3-wire device
* @param Reg_Addr: Register address, Reg_Data: Data to write-to
* @return None
****************************************************************/
void TriWire_Write(uint8 Reg_Addr, uint8 Reg_Data)
{
TriWire_RST = LOW;
TriWire_SCLK = LOW;
TriWire_RST = HIGH;
delay_time(TriWireDELAY);
TriWire_WriteByte(Reg_Addr);
TriWire_WriteByte(Reg_Data);
TriWire_SCLK = HIGH;
TriWire_RST = LOW;
}
/*****************************************************************
* @fn TriWire_Read
* @brief Read data from the register on a 3-wire device
* @param Reg_Addr: Register address
* @return Data: The read-in data byte@Reg_Addr
****************************************************************/
uint8 TriWire_Read(uint8 Reg_Addr)
{
uint8 Data;
TriWire_RST = LOW;
TriWire_SCLK = LOW;
TriWire_RST = HIGH;
delay_time(TriWireDELAY);
TriWire_WriteByte(Reg_Addr|0x01); // Command - Read
Data = TriWire_ReadByte();
TriWire_SCLK = HIGH;
TriWire_RST = LOW;
return(Data);
}
#if (HAL_TRIWIRE == TRUE)
…
#endif
114. /***************************************************************************************************
* @fn HalTimerConfig
* @brief Configure the Timer Serivce
* @param timerId - Id of the timer, opMode - Operation mode, cBack - Pointer to callback function
* @return Status of the configuration
***************************************************************************************************/
uint8 HalTimerConfig (uint8 timerId, uint8 opMode, bool intEnable, halTimerCBack_t cBack)
{
if (timerId < HAL_TIMER_MAX) {
halTimerRecord[timerId].configured = TRUE;
halTimerRecord[timerId].opMode = opMode;
halTimerRecord[timerId].intEnable = intEnable;
halTimerRecord[timerId].TH = 0;
halTimerRecord[timerId].TL = 0;
halTimerRecord[timerId].callBackFunc = cBack;
} else {
return HAL_TIMER_PARAMS_ERROR;
}
return HAL_TIMER_OK;
}
/******** FUNCTIONS – API ********/
/*******************************************************************
* @fn HalTimerInit
* @brief Initialize Timer Service
* @param None
* @return None
*******************************************************************/
void HalTimerInit (void)
{ // disable all timer interrupts
IE &= ~(IE_T0_IntEn_Bit|IE_T1_IntEn_Bit);
/* Setup prescale value & clock for timer */
halTimerRecord[HAL_TIMER_0].clock = HAL_TIMER_12MHZ;
halTimerRecord[HAL_TIMER_0].prescaleVal = HAL_TIMER_PRESCALE_VAL;
halTimerRecord[HAL_TIMER_1].clock = HAL_TIMER_12MHZ;
halTimerRecord[HAL_TIMER_1].prescaleVal = HAL_TIMER_PRESCALE_VAL;
}
114
115. /*************************************************************************************************
* @fn HalTimerStart
* @brief Start the Timer Service
* @param timerId - ID of the timer
* timerPerTick - number of micro sec per tick, (ticks x prescale) / clock = usec/tick
* @return Status - OK or Not OK
*************************************************************************************************/
uint8 HalTimerStart (uint8 timerId, uint16 timePerTick)
{
if (halTimerRecord[timerId].configured) {
halTimerSetOpMode (timerId, halTimerRecord[timerId].opMode);
halTimerSetCount (timerId, timePerTick);
HalTimerInterruptEnable(timerId, halTimerRecord[timerId].intEnable);
if (timerId == HAL_TIMER_0) TCON |= TCON_T0_START_BV;
if (timerId == HAL_TIMER_1) TCON |= TCON_T1_START_BV;
} else {
return HAL_TIMER_NOT_CONFIGURED;
}
return HAL_TIMER_OK;
}
/***************************************************
* @fn HalTimerStop
* @brief Stop the Timer Service
* @param timerId - ID of the timer
* @return Status - OK or Not OK
**************************************************/
uint8 HalTimerStop (uint8 timerId)
{
switch(timerId)
{
case HAL_TIMER_0:
TCON &= ~(TCON_T0_START_BV); break;
case HAL_TIMER_1:
TCON &= ~(TCON_T1_START_BV); break;
default:
return HAL_TIMER_INVALID_ID;
}
return HAL_TIMER_OK;
} 115