Skip to content

Instantly share code, notes, and snippets.

@cosmith
Created December 8, 2014 09:44
Show Gist options
  • Save cosmith/f1c8ecb7214505ff4bbf to your computer and use it in GitHub Desktop.
Save cosmith/f1c8ecb7214505ff4bbf to your computer and use it in GitHub Desktop.
//-----------------------------------------------------------------------------
// F31x_Blinky.c
//-----------------------------------------------------------------------------
// Copyright (C) 2007 Silicon Laboratories, Inc.
//
// AUTH: JS
// DATE: 03 JUL 02
//
// This program flashes the green LED on the C8051F31x target board about
// five times a second using the interrupt handler for Timer2.
//
// Target: C8051F31x
//
// Tool chain: KEIL Eval 'c'
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <SI_C8051F310_Register_Enums.h> // SFR declarations
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 24500000 / 8 // SYSCLK frequency in Hz
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void PORT_Init (void);
void Put_char_(unsigned char c);
void Timer2_Init (int counts);
void Timer2_ISR (void);
sbit LED = P3^3; // LED='1' means ON
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
// disable watchdog timer
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
// enable)
SYSCLK_Init(); // Initialize system clock
PORT_Init(); // Initialize crossbar and GPIO
IE_EA = 1; // enable global interrupts
while (1) { // spin forever
Put_char_(' ');
}
}
void Put_char_(unsigned char c)
{
while (SCON0 | SCON0_TI__BMASK == 0);
SCON0 |= SCON0_TI__NOT_SET;
SBUF0 = c;
}
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use the internal 24.5MHz / 8
// oscillator as its clock source. Also enables missing clock detector reset.
//
void SYSCLK_Init (void)
{
OSCICN = 0xc3; // configure internal oscillator for
// its lowest frequency
RSTSRC = 0x04; // enable missing clock detector
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports.
// P3.3 - LED (push-pull)
//
void PORT_Init (void)
{
XBR0 = 0x01;
XBR1 = 0x40; // Enable crossbar and weak pull-ups
P3MDOUT |= 0x08;
P0MDOUT |= 0x10;
}
void UART0_Init()
{
TH1 = -213;
TMOD |= 0x20;
CKCON |= 0x08;
TCON_TR1 = 1;
SCON0 |= SCON0_REN__RECEIVE_ENABLED;
SBUF0 = '\n';
}
void Timer2_Init (int counts)
{
TMR2CN = 0x00; // Stop Timer2; Clear TF2;
// use SYSCLK/12 as timebase
CKCON &= ~0x60; // Timer2 clocked based on T2XCLK;
TMR2RL = -counts; // init reload values
TMR2 = 0xffff; // set to reload immediately
IE_ET2 = 1; // enable Timer2 interrupts
TMR2CN_TR2 = 1; // start Timer2
}
//-----------------------------------------------------------------------------
// Timer2_ISR
//-----------------------------------------------------------------------------
// This routine changes the state of the LED whenever Timer2 overflows.
//
INTERRUPT(Timer2_ISR, 5)
{
TMR2CN_TF2H = 0; // clear Timer2 interrupt flag
LED = ~LED; // change state of LED
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment