Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Header file with all the essential definitions for a given type of MCU */
- #include "MK60D10.h"
- /* Macros for bit-level registers manipulation */
- #define GPIO_PIN_MASK 0x1Fu
- #define GPIO_PIN(x) (((1)<<(x & GPIO_PIN_MASK)))
- /* Constants specifying delay loop duration */
- #define DELAY_T 200000
- /* Mapping of LEDs and buttons to specific port pins: */
- // Note: only D9, SW3 and SW5 are used in this sample app
- #define LED_D9 0x20 // Port B, bit 5
- #define LED_D10 0x10 // Port B, bit 4
- #define LED_D11 0x8 // Port B, bit 3
- #define LED_D12 0x4 // Port B, bit 2
- #define BTN_SW2 0x400 // Port E, bit 10
- #define BTN_SW3 0x1000 // Port E, bit 12
- #define BTN_SW4 0x8000000 // Port E, bit 27
- #define BTN_SW5 0x4000000 // Port E, bit 26
- #define BTN_SW6 0x800 // Port E, bit 11
- int pressed_up = 0, pressed_down = 0;
- unsigned int compare = 0x1000;
- unsigned char c; // prijaty znak
- unsigned char prompt[7] = " login>"; // text vyzvy
- /* A delay function */
- void delay(long long bound) {
- long long i;
- for(i=0;i<bound;i++);
- }
- /* Initialize the MCU - basic clock settings, turning the watchdog off */
- void MCUInit(void) {
- MCG_C4 |= ( MCG_C4_DMX32_MASK | MCG_C4_DRST_DRS(0x01) );
- SIM_CLKDIV1 |= SIM_CLKDIV1_OUTDIV1(0x00);
- WDOG_STCTRLH &= ~WDOG_STCTRLH_WDOGEN_MASK;
- }
- void PortsInit(void)
- {
- /* Turn on all port clocks */
- SIM->SCGC5 = SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTE_MASK;
- /* Set corresponding PTB pins (connected to LED's) for GPIO functionality */
- PORTB->PCR[5] = PORT_PCR_MUX(0x01); // D9
- PORTB->PCR[4] = PORT_PCR_MUX(0x01); // D10
- PORTB->PCR[3] = PORT_PCR_MUX(0x01); // D11
- PORTB->PCR[2] = PORT_PCR_MUX(0x01); // D12
- PORTE->PCR[10] = PORT_PCR_MUX(0x01); // SW2
- PORTE->PCR[12] = PORT_PCR_MUX(0x01); // SW3
- PORTE->PCR[27] = PORT_PCR_MUX(0x01); // SW4
- PORTE->PCR[26] = PORT_PCR_MUX(0x01); // SW5
- PORTE->PCR[11] = PORT_PCR_MUX(0x01); // SW6
- /* Change corresponding PTB port pins as outputs */
- PTB->PDDR = GPIO_PDDR_PDD(0x3C);
- PTB->PDOR |= GPIO_PDOR_PDO(0x3C); // turn all LEDs OFF
- }
- /* Inicializace pinu pro vysilani a prijem pres UART - RX a TX */
- void PinInit(void) {
- SIM->SOPT2 |= SIM_SOPT2_UART0SRC(0x01);
- SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK; // Zapnout hodiny pro PORTA a PORTB
- SIM->SCGC4 |= SIM_SCGC4_UART0_MASK;
- PORTB->PCR[1] = ( 0 | PORT_PCR_MUX(0x02) ); // UART0_TX
- PORTB->PCR[2] = ( 0 | PORT_PCR_MUX(0x02) ); // UART0_RX
- PORTB->PCR[13] = ( 0|PORT_PCR_MUX(0x01) ); // Beeper (PTB13)
- PTB->PDDR = GPIO_PDDR_PDD( 0x2000 ); // "1" znamena, ze pin bude vystupni
- }
- /* Inicializace UART - nastaveni prenosove rychlosti 115200Bd, 8 bitu, bez parity */
- void UART0Init(void) {
- /*UART0->C2 &= ~(UART0_C2_TE_MASK | UART0_C2_RE_MASK);
- UART0->BDH = 0x00;
- UART0->BDL = 0x1A; // Baud rate 115 200 Bd, 1 stop bit
- UART0->C4 = 0x0F; // Oversampling ratio 16, match address mode disabled
- UART0->C1 = 0x00; // 8 data bitu, bez parity
- UART0->C3 = 0x00;
- UART0->MA1 = 0x00; // no match address (mode disabled in C4)
- UART0->MA2 = 0x00; // no match address (mode disabled in C4)
- UART0->S1 |= 0x1F;
- UART0->S2 |= 0xC0;
- UART0->C2 |= ( UART0_C2_TE_MASK | UART0_C2_RE_MASK ); // Zapnout vysilac i prijimac*/
- }
- /* vyslani jednoho znaku (ch) pres UART - funkce vycka, az je vysilaci buffer prazdny, pak posle */
- /*void SendCh(char ch) {
- while(!(UART0->S1 & UART0_S1_TDRE_MASK) && !(UART0->S1 & UART0_S1_TC_MASK) );
- UART0->D = ch;
- }
- */
- /* prijeti jednoho znaku pres UART - funkce ceka na prichozi znak a ten vrati jako vysledek */
- /* !!!!! FUNKCI DOPLNTE */
- /*
- char ReceiveCh(void) {
- while(!(UART0->S1 & UART0_S1_RDRF_MASK));
- return UART0->D;
- }
- */
- /* vysilani retezce ukonceneho 0 */
- void SendStr(char *s) {
- int i = 0;
- while (s[i]!=0){
- //SendCh(s[i++]);
- }
- }
- void RTC_IRQHandler(void) {
- if((RTC_SR & RTC_SR_TIF_MASK)== 0x01)
- {
- RTC_SR &= 0x07; //clear TCE, or RTC_TSR can not be written
- RTC_TSR = 0x00000000; //clear TIF
- }
- else if((RTC_SR & RTC_SR_TOF_MASK) == 0x02)
- {
- RTC_SR &= 0x07; //clear TCE, or RTC_TSR can not be written
- RTC_TSR = 0x00000000; //clear TOF
- }
- else if((RTC_SR & RTC_SR_TAF_MASK) == 0x04)
- {
- GPIOB_PDOR ^= LED_D9; // invert D9 state
- RTC_TAR += 5;// Write new alarm value, to generate an alarm every second add 1
- }
- else
- {
- }
- return;
- }
- void RTCInit(void)
- {
- int i;
- /*enable the clock to SRTC module register space*/
- SIM_SCGC6 |= SIM_SCGC6_RTC_MASK;
- /*Only VBAT_POR has an effect on the SRTC, RESET to the part does not, so you must manually reset the SRTC to make sure everything is in a known state*/
- /*clear the software reset bit*/
- RTC_CR = RTC_CR_SWR_MASK;
- RTC_CR &= ~RTC_CR_SWR_MASK;
- /*Enable the interrupt*/
- NVIC_EnableIRQ(RTC_IRQn);
- /*Enable the oscillator*/
- RTC_CR |= RTC_CR_OSCE_MASK;
- /*Wait to all the 32 kHz to stabilize, refer to the crystal startup time in the crystal datasheet*/
- delay(6000000);
- /*Set time compensation parameters*/
- RTC_TCR = 0;
- /*Configure the timer seconds and alarm registers*/
- RTC_TSR = 0;
- RTC_TAR = 0;
- /*Enable the counter*/
- //RTC_SR |= RTC_SR_TCE_MASK;
- }
- void RTCStop(void){
- BITBAND_REG(RTC->SR, RTC_SR_TCE_SHIFT) = 0;
- }
- void RTCStart(void){
- BITBAND_REG(RTC->SR, RTC_SR_TCE_SHIFT) = 1;
- }
- void RTCSetTime(int seconds){
- RTC->TSR = seconds;
- }
- void RTCSetAlarm(int seconds){
- RTC->TAR = seconds;
- }
- int main(void)
- {
- MCUInit();
- PortsInit();
- long long a = RTC->CR;
- RTCInit();
- //GPIOB_PDOR ^= LED_D9; // invert D9 state
- while (1) {
- SendStr("fuckyou");
- /* // pressing the up button decreases the compare value,
- // i.e. the compare event will occur more often;
- // testing pressed_up avoids uncontrolled modification
- // of compare if the button is hold.
- if (!pressed_up && !(GPIOE_PDIR & BTN_SW5))
- {
- pressed_up = 1;
- compare -= 0x40;
- }
- else if (GPIOE_PDIR & BTN_SW5) pressed_up = 0;
- // pressing the down button increases the compare value,
- // i.e. the compare event will occur less often;
- if (!pressed_down && !(GPIOE_PDIR & BTN_SW3))
- {
- pressed_down = 1;
- compare += 0x40;
- }
- else if (GPIOE_PDIR & BTN_SW3) pressed_down = 0;
- // some limits - in order to keep the LED blinking reasonable*/
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement