📄 periphery.c
字号:
/*
* File: periphery.c
* Purpose: Keyboard and joystick handler functions
* Author: Peter Ivanov, Olimex Ltd.
* Modified by:
* Created: 2007-05-19 11:29:32
* Last modify: 2007-10-03 21:05:09 ivanovp {Time-stamp}
* Copyright: (C) Peter Ivanov, Olimex Ltd., 2007
* Licence: GPL
*/
/**
* \file periphery.c
* \brief Keyboard and joystick handler functions
* \author Peter Ivanov, Olimex Ltd.
*/
#include <msp430xG461x.h>
#include <signal.h>
#include "periphery.h"
#include "lcd.h"
#include "time.h"
volatile bool_t KBD_b1Pressed;
volatile bool_t KBD_b2Pressed;
/**
* Get B1 status
*
* @return TRUE: if button pressed.
* FALSE: if button not pressed.
*/
inline uint8_t KBD_getB1 ()
{
return !(P1IN & BIT6);
}
/**
* Get B2 status
*
* @return TRUE: if button pressed.
* FALSE: if button not pressed.
*/
inline uint8_t KBD_getB2 ()
{
return !(P1IN & BIT7);
}
/**
* Interrupt routine for Port1
*/
//#pragma vector=PORT1_VECTOR
//__interrupt void handle_port_interrupt( void )
interrupt (PORT1_VECTOR) handle_port_interrupt( void )
{
// Button 1 (B1)
if (P1IFG & BIT6)
{
KBD_b1Pressed = TRUE;
}
// Button 2 (B2)
if (P1IFG & BIT7)
{
KBD_b2Pressed = TRUE;
}
// Clear all interupt flag
P1IFG = 0x0;
P2IFG = 0x0;
}
/**
* Initialize buttons B1 and B2
*/
void KBD_init (void)
{
// Ports P1.6, P1.7 as input
P1DIR &= ~(BIT6 | BIT7);
// Set high-to-low transition for P1.6, P1.7
P1IES |= BIT6 | BIT7;
// Enable interupts for P1.6, P1.7
P1IE |= BIT6 | BIT7;
// Clear all interrupts flag
P1IFG = 0x0;
P2IFG = 0x0;
}
/**
* Initialize Joystick Button
*/
void JOY_init(void)
{
// Init ADC interface
ADC12_init ();
// A7 as analog input
P6SEL |= BIT7; // P6.7 ADC10 function
P6DIR &= ~BIT7; // P6.7 input direction
// P2.6 as input
P2SEL &= ~BIT6; // P2.6 as GPIO
P2DIR &= ~BIT6; // P2.6 as input
}
/**
* Calculates joystick position. Use function KBD_getButtonPressed()
* instead of this one!
* Note: presumably you should update the numbers below.
*
* @return Actual status of joystick.
*/
JOY_pos_t JOY_getPosition (void)
{
JOY_pos_t jresult;
int adcresult;
char nsample;
adcresult = 0;
jresult = 0;
for (nsample = 0; nsample < 8; nsample++)
{
adcresult += ADC12_get (INCH_7);
}
adcresult /= (nsample + 1);
// For calibrating joystick
//LCD_setXY (0, 0);
//LCD_printf ("Joystick: %04i", adcresult);
// these values are measured experimentaly
if ((adcresult > 1800) && (adcresult < 1950))
{
jresult += JOY_POS_LEFT;
}
else if ((adcresult > 1100) && (adcresult < 1250))
{
jresult += JOY_POS_RIGHT;
}
else if ((adcresult > 2000) && (adcresult < 2150))
{
jresult += JOY_POS_UP;
}
else if ((adcresult > 600) && (adcresult < 750))
{
jresult += JOY_POS_DOWN;
}
if ((P2IN & BIT6) != 0)
{
jresult += JOY_POS_CENTER;
}
return jresult;
}
KBD_buttonPressed_t KBD_getButtonPressed ()
{
KBD_buttonPressed_t buttonPressed;
static JOY_pos_t prevJoyPos = 0;
JOY_pos_t joyPos;
// Polling joystick position
joyPos = JOY_getPosition ();
buttonPressed = (prevJoyPos ^ joyPos) & joyPos;
prevJoyPos = joyPos;
if (KBD_b1Pressed)
{
buttonPressed |= KBD_B1;
KBD_b1Pressed = FALSE;
}
if (KBD_b2Pressed)
{
buttonPressed |= KBD_B2;
KBD_b2Pressed = FALSE;
}
return buttonPressed;
}
/**
* Initialize GPIO port
*/
void GPIO_init(void)
{
// all as GPIO
P1SEL = 0x00;
P2SEL = 0x00;
P3SEL = 0x00;
P4SEL = 0x00;
P5SEL = 0x00;
P6SEL = 0x00;
P7SEL = 0x00;
P8SEL = 0x00;
P9SEL = 0x00;
P10SEL = 0x00;
// all as low
P1OUT = 0x00;
P2OUT = 0x00;
P3OUT = 0x00;
P4OUT = 0x00;
P5OUT = 0x00;
P6OUT = 0x00;
P7OUT = 0x00;
P8OUT = 0x00;
P9OUT = 0x00;
P10OUT = 0x00;
// all as output
P1DIR = 0xFF;
P2DIR = 0xFF;
P3DIR = 0xFF;
P4DIR = 0xFF;
P5DIR = 0xFF;
P6DIR = 0xFF;
P7DIR = 0xFF;
P8DIR = 0xFF;
P9DIR = 0xFF;
P10DIR = 0xFF;
}
/**
* Initialize ADC12 module
*/
void ADC12_init (void)
{
/* ADC12 Control Register 0 */
// ADC12 sample-and-hold time -> 256 x ADC12CLKs,
// Reference = 2.5V, Select ADC12MEM0
ADC12CTL0 = SHT03; // orig!
//ADC12CTL0 = REFON | REF2_5V;
// Sample-and-hold source select -> ADC12SC bit, no clock divider,
// clock -> ADC12OSC - internal osillator ~5MHz, Single-channel and single-conversion
// ADC12CTL1 = SHP;
// Sample-and-hold source select -> ADC12SC bit, no clock divider,
// clock -> MCLK - external osillator ~2MHz, Single-channel and single-conversion
//ADC12CTL1 = ADC12SSEL1 | SHP;
ADC12CTL1 = ADC12SSEL1; // orig!
// Conversion Memory Control Registers
// Select reference -> VR+ = AVCC+ and VR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -