summaryrefslogtreecommitdiff
path: root/rs422lib/main.c
blob: 2a2517f664147fd8f20b1896fa0e44fcbfe054bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include"precomp.h"
#include"rsbus.h"
#include"sysctl.h"

int cnt=0;

void port_init(void)
{
    P1DIR |= BIT0+BIT6;      // P1.0 P1.6 output
    P1OUT = 0;
}

void received(unsigned char* str,unsigned int len)
{
	rsbus_w(0,str,len);
}

// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A0 (void)
{
	++cnt;
	if (cnt==25)
	{
		STATE(0)=(STATE(0)+1)&0xFF;
		cnt=0;
		P1OUT^=BIT6;
	}
}

//TIMER A0 initialize -
// desired value: 5ms
void TimerA0_Init(void)
{
	// Configure TimerA0
	TA0CTL = TASSEL_2 + MC_1 +ID_3 ;      // Source: SMCLK=4MHz, UP mode, DIV by 8 -> 0.5M
	TA0CCR0 = 20000;					// 0.5MHz / 20000 -> 25Hz -> 40ms
	TA0CCTL0 = CCIE;                  	// CCR0 interrupt enabled
}

void init_devices(void)
{
	WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer

	if (CALBC1_8MHZ ==0xFF || CALDCO_8MHZ == 0xFF)
		while(1)_BIS_SR(CPUOFF);            	// If calibration constants erased, trap CPU!!

	BCSCTL1 = CALBC1_16MHZ; 		    // Set range
	DCOCTL = CALDCO_16MHZ;  		    // Set DCO step + modulation£¬DCO=8MHz
	BCSCTL3 |= LFXT1S_2;                      // LFXT1 = VLO
	IFG1 &= ~OFIFG;                           // Clear OSCFault flag
	BCSCTL2 |= DIVS_2;      //  SMCLK = DCO/4 = 4MHz

	sysctl_init();
	port_init();
	TimerA0_Init();
	rsbus_init(&received);

	_BIS_SR(GIE);
}

void main(void)
{
	init_devices();
	while(1)
		sysroutine();
}