92 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			92 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * time.c
 | |
|  *
 | |
|  *  Created on: 17.01.2016
 | |
|  *      Author: wn
 | |
|  */
 | |
| #include <msp430g2553.h>
 | |
| #include <isr_compat.h>
 | |
| #include <stdint.h>
 | |
| 
 | |
| #include "time.h"
 | |
| 
 | |
| 
 | |
| volatile uint16_t timestamp = 0;
 | |
| volatile uint32_t tickCnt = 0;
 | |
| const uint16_t TICK = 1024;
 | |
| uint16_t pwmVal = 128;
 | |
| volatile uint16_t onTime = 500;
 | |
| volatile uint16_t offTime = 200;
 | |
| 
 | |
| ISR(TIMER0_A0, TA0_ISR) {
 | |
|     tickCnt++;
 | |
| 
 | |
| 	static uint8_t state0 = 0;
 | |
| 	static uint8_t state1 = 0;
 | |
| 
 | |
| 	switch (state0) {
 | |
| 	case 0:
 | |
| 		P1OUT |= BIT0;
 | |
| 		state0 = 1;
 | |
| 		break;
 | |
| 	case 1:
 | |
| 		P1OUT &= ~BIT0;
 | |
| 		state0 = 0;
 | |
| 		break;
 | |
| 	default:
 | |
| 		state0 = 0;
 | |
| 	}
 | |
| 
 | |
| 	timestamp++;
 | |
| 	switch (state1) {
 | |
| 	case 0:
 | |
| 		if (timestamp >= onTime) {
 | |
| 			timestamp = 0;
 | |
| 			P1OUT |= BIT1;
 | |
| 			P1SEL &= ~BIT6;
 | |
| 			P1OUT &= ~BIT6;
 | |
| 			state1 = 1;
 | |
| 		}
 | |
| 		break;
 | |
| 	case 1:
 | |
| 		if (timestamp >= offTime) {
 | |
| 			timestamp = 0;
 | |
| 			P1OUT &= ~BIT1;
 | |
| 			P1SEL |= BIT6;
 | |
| 			state1 = 0;
 | |
| 		}
 | |
| 		break;
 | |
| 	default:
 | |
| 		state1 = 0;
 | |
| 	}
 | |
| 
 | |
| 
 | |
| }
 | |
| 
 | |
| 
 | |
| void timeInit() {
 | |
| 	timestamp = 0;
 | |
| 
 | |
| 	P1DIR |= BIT0 | BIT1 | BIT6;
 | |
| 	P1OUT = 0;
 | |
| 	P1SEL |= BIT6;
 | |
| 
 | |
| 	TACCR0 = TICK;
 | |
| 	TACCR1 = pwmVal;
 | |
| 
 | |
| 	TACCTL0 = CCIE;
 | |
| 	TACCTL1 = OUTMOD_7;
 | |
| 	TACTL = MC_1 | ID_0 | TASSEL_2 | TACLR | TAIE;
 | |
| }
 | |
| 
 | |
| 
 | |
| uint32_t ticks() {
 | |
|     __disable_interrupt();
 | |
|     uint32_t t = tickCnt;
 | |
|     __enable_interrupt();
 | |
|     
 | |
|     return t;
 | |
| }
 | |
| 
 | |
| 
 | 
