#include "IRRemote.h"
void main(void)
{
//初始化
Initialize();
//Infinite loopwhile
(1)
{
unsigned int transmit;
//Initialize variable
transmit = ENDTRANSMIT;
//Wait for a key to be pressed
SetForPress();
//Enter LPM4 mode
LPM4;
Debounce();
//Scan keyboard
KeyS CAN ();
//Keyboard processing
KeyLookup();
if (Error_Flags == 0)
{
//If a key is pressed
SetupForRelease();
do
{
//Transmit data (RC5 code)
Transmit();
transmit = TestRetransmit();
//Delay
DelayToNextTransmit();
} while (transmit == RETRANSMIT);
}
}
}
void Initialize(void)
{
//Stop watchdog
WDTCTL = WDTPW+WDTHOLD;
//P2 port is output
P2DIR = 0xFF;
//P2.3 is TA1 function pin
P2SEL = 0x08;
//Clear P2 port output
P2OUT = 0;
//Set DCO frequency to 1M Hz
DCOCTL = CALDCO_1MHZ;
BCSCTL1 = CALBC1_1MHZ;
//Enable interrupt_EINT
();
}
void SetForPress(void)
{
//P1.0 and P1.1 are inputs
P1DIR = 0xFC;
//Clear the output of P1 port, pull down
P1OUT = 0;
//Enable the resistors
of P1.0 and P1.1 P1REN |= 0x03;
//Enable keyboard
P2OUT |= 0x07;
//Low to high interrupt mode
P1IES &= ~0x03;
//Clear interrupt flag
P1IFG = 0;
//Enable interrupt
P1IE |= 0x03;
//Clear error flag
Error_Flags = 0;
//Clear transmission flag
Trans_Flags = 0;
}
void Debounce(void)
{
//SMCLK/8, clear TA
TACTL = TASSEL1+TACLR+ID0+ID1;
//Enable CC R0 interrupt
TACCTL0 = CCIE;
//Set the delay value
TACCR0 = 5000 - 1;
//Start UP mode
TACTL |= MC0;
//Enter LPM0 mode
LPM0;
//Stop and clear
TACTL = TACLR;
//Clear CCTL0 register
TACCTL0 = 0;
}
void KeyScan(void)
{
unsigned int i;
//Initialize row
RowMask = 0x01;
KeyHex = 0;
//Clear row value
P1OUT &= ~0x03;
for (i=0; i < 3; i++)
{
P2OUT &= ~0x07;
P1DIR |= 0x03;
P1OUT &= ~0x03;
P1DIR &= ~0x03;
P2OUT = RowMask;
if (P1IN & 0x03)
{
//Is there a key pressedKeyHex
|= RowMask;
//Processing valueKeyHex
|= (P1IN & 0x03) << 4;
}
//Next
rowRowMask = RowMask << 1;
}
if (KeyHex == 0)
{
//Set
flagError_Flags |= noKey;
}
P2OUT |= 0x07;
}
void KeyLookup(void)
{
unsigned int i;
//Initialization
valueKeyPressed = 99;
for (i = 0; i < 6; i++)
{
//Lookup tableif
(KeyTab[i] == KeyHex)
{
KeyPressed = i;
}
}
if (KeyPressed == 99)
{
//Value not found
//Set flags
Error_Flags |= noMatch;
}
}
void SetupForRelease(void)
{
//Set direction
P1DIR |= 0x03;
P1OUT |= 0x03;
//Key pressed
Trans_Flags |= h ELD Down;
}
void DetermineRelease(void)
{
//Set input pull-down
P1OUT &= ~0x03;
P1DIR &= ~0x03;
if (P1IN & 0x03)
{
SetupForRelease();
}
else
{
Trans_Flags &= ~heldDown;
}
}
void Transmit(void)
{
unsigned int i;
//Get command code
Command = FuncTab[KeyPressed];
//Increase the start bit by two bits
Command = Command + 0x3000;
if (Trans_Flags & toggle)
{
// Toggle bit
Command = Command + 0x0800;
}
//Left shift
Command = Command << 2;
//CCR1 PWM parameter
TACCR1 = 7;
//Enable CCR0 interrupt
TACCTL0 = CCIE;
for (i=0; i < 14; i++)
{
//Judge whether it is 1 or 0
if (Command & 0x8000)
{
//First send 780us low
OutputLow(780);
//Then high, count 32 interrupts
OutputHigh(32);
}
else
{
//First high, count 35 interrupts
OutputHigh(35);
OutputLow(765);
}
//Next bit
Command = Command << 1;
}
//Stop and clear TA
TACTL = TACLR;
}
unsigned int TestRetransmit(void)
{
//Is there any input in P1
if (P1IFG == 0)
{
//If there is no input,
return RETRANSMIT;
}
else
{
//If there is input,
//Is there a key pressed
DetermineRelease();
if (Trans_Flags & heldDown)
{
//If there is ,
return RETRANSMIT;
}
else
{
//If there is no input, return to the main program
//Clear CCTL0 register
TACCTL0 = 0;
//Clear CCTL1 register
TACCTL1 = 0;
//Clear toggle bit
Trans_Flags &= ~toggle;
return ENDTRANSMIT;
}
}
}
void DelayToNextTransmit(void)
{
//SMCLK/8 clear TA
TACTL = TASSEL1+TACLR+ID0+ID1;
//89ms interrupt
TACCR0 = 11125 - 1;
//Enable CCR0 interrupt
TACCTL0 = CCIE;
//TA starts UP mode
TACTL |= MC0;
//Delay
LPM0;
//Stop and clear TA
TACTL = TACLR;
//Switch the switch bit
Trans_Flags ^= toggle;
}
void OutputHigh(unsigned int val)
{
unsigned int j;
//CCR1 RESET/SET
TACCTL1 = OUTMOD_7;
//PWM period (40kHz)
TACCR0 = 25 - 1;
//Clear TA
TACTL = TASSEL1+TACLR;
//TA starts UP mode
TACTL |= MC0;
for (j = val; j > 0; j--)
{
//Interrupt counting
//When the period is 40kHz, the time of each interrupt is 25us
LPM0;
}
}
void OutputLow(unsigned int val)
{
// Set CC1 output to 0
TACCTL1 = OUTMOD_0;
// Clear TA
TACTL = TASSEL1+TACLR;
// Delay
TACCR0 = val;
// Set TA to UP mode
TACTL |= MC0;
// Enter LPM0 mode, wait for CCR0 interrupt
LPM0;
}
// P1.x interrupt service routine
#if __VER__ < 200
interrupt [PORT1_VECTOR] void P1_ISR(void)
#else
#pragma vector=PORT1_VECTOR
__interrupt void P1_ISR(void)
#endif
{
//Exit LPM4 low power mode
LPM4_EXIT;
//Clear interrupt flag
P1IFG = 0;
//Disable interrupt
P1IE = 0;
}
// CCR0 interrupt service routine
#if __VER__ < 200
interrupt [TIMERA0_VECTOR] void CCR0_ISR(void)
#else
#pragma vector=TIMERA0_VECTOR
__interrupt void CCR0_ISR(void)
#endif
{
//Exit LPM0 low power mode
LPM0_EXIT;
}
IRRemote.h
#include " MSP 430x21x1.h"
#define noKey 0x001
#define noMatch 0x002
#define heldDown 0x001
#define toggle 0x002
#define RETRANSMIT 1
#define ENDTRANSMIT 0
// Key Lookup Table
const char KeyTab[6] = {
0x11, // Key 0
0x21, // Key 1
0x12, // Key 2
0x22, // Key 3
0x14, // Key 4
0x24 // Key 5
};
// Function Lookup Table
const char FuncTab[6] = {
0x0C, // Key 0 - Power
0x0D, // Key 1 - Mute
0x20, // Key 2 - Channel +
0x10, // Key 3 - Volume +
0x21, // Button 4 - Channel -
0x11 // Button 5 - Volume -
};
unsigned int RowMask;
unsigned int KeyHex;
unsigned int KeyVal;
unsigned int KeyPressed;
unsigned int Command;
unsigned int Trans_Flags;
unsigned int Error_Flags;
void Initialize(void);
void SetForPress(void);
void Debounce( void);
void KeyScan(void);
void KeyLookup(void);
void SetupForRelease(void);
void DetermineRelease(void);
void Transmit(void);
unsigned int TestRetransmit(void);
void DelayToNextTransmit(void);
void OutputHigh(unsigned int);
void OutputLow(unsigned int);
Previous article:Domestic blood glucose meter based on MSP430F435
Next article:MSP430 MCU key program
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- FPGA Entry Series Digital Tube
- Analog Dialogue Volume 55 Issue 3 is now online! Come and get your source of inspiration~
- STM8 Little Frog: Return to the Arena
- What are some good ways to transform various periodic waveforms into square waves that can be recognized by microcontrollers?
- General Design Considerations for Embedded Systems
- CH340 circuit improves working stability and anti-interference
- Schematic diagram of numworks
- What are the similarities and differences between the IO port simulation serial port, PWM, IIC functions and the serial port, PWM, IIC built into the chip hardware?
- 【Multi-function open source custom macro keyboard】OLED display test
- SOPC technology and Nios-II soft-core processor