Skip to content

Instantly share code, notes, and snippets.

@ajarmst
Created March 15, 2022 16:23
Show Gist options
  • Save ajarmst/a1db718662d9386adad76a957700bbbd to your computer and use it in GitHub Desktop.
Save ajarmst/a1db718662d9386adad76a957700bbbd to your computer and use it in GitHub Desktop.
/////////////////////////////////////////////////////////////////////////////
// 9S12X Program: YourProg - MiniExplanation
// Processor: MC9S12XDP512
// Bus Speed: 20 MHz (Requires Active PLL)
// Author: Simon Walker
// Details: A more detailed explanation of the program is entered here
// Date: Date Created
// Revision History :
// each revision will have a date + desc. of changes
/////////////////////////////////////////////////////////////////////////////
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include "pll.h"
#include "swled.h"
//#include <stdlib.h>
//#include <stdio.h>
#define SEGWSTROBE PORTA &= (~0x01); PORTA |= 0x01;
#define SEGMCOMM PORTA |= 0x02;
#define SEGMDATA PORTA &= (~0x02);
/////////////////////////////////////////////////////////////////////////////
// Local Prototypes
/////////////////////////////////////////////////////////////////////////////
int SEGNormal(byte Addr, byte Value, int DPOn);
int SEGCustom(byte Addr, byte Value, int DPOn);
int SEGCodeB(byte Addr, byte Value);
int SEGClear(byte Addr);
int SEGClearAll(void);
int SEGHexLine2(uint Value); //Display value in hex (<=FFFF)
int SEGDecLine2(uint Value); //Display value in decimal (<=9999)
/////////////////////////////////////////////////////////////////////////////
// Global Variables
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Constants
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Main Entry
/////////////////////////////////////////////////////////////////////////////
void main(void)
{
uint count = 0;
// main entry point - these two lines must appear first
_DISABLE_COP();
EnableInterrupts;
/////////////////////////////////////////////////////////////////////////////
// one-time initializations
/////////////////////////////////////////////////////////////////////////////
PLL_To20MHz();
(void) SWLInit();
//Initialize Timer. OC0
// Turn the thing on
TSCR1 = 0b10000000; //TSCR1_TEN is bit 7. Turn it on. 7.3.2.6
// Set Prescale of 128
TSCR2 = 7; //0b00000111; //Timer rate = BUSSPD/2^7 = 20 MHz/128 = 156.25 KHz (6.4 us tick)
// Use channel 0 OC
TIOS |= 0b00000001; //1
// Toggle the pin for channel 0 (pin 9)
TCTL2 = 0b0000001;
//Timer for OC0 is currently set up. Let's use it.
// 1 - Clear any pending flags
TFLG1 = 1; // Yes, 1 clears. Not 0
//Arm for an event in 100 ms
TC0 = TCNT + 15625;// 100 ms / 6.4 us = 15625
//Initialize the 7Segs
PORTA |= 0x03; // Set the output state of the Mode and Write lines to high
DDRA |= 0x03; // Set them as GPIO outputs
DDRB = 0xFF; //Set all of Port B as output (data/command bus)
//Later, I'll want to clear all the segments
(void) SEGClearAll();
/////////////////////////////////////////////////////////////////////////////
// main program loop
/////////////////////////////////////////////////////////////////////////////
for (;;)
{
(void) SEGNormal(0,0xA,0);
(void) SEGCustom(1,0b00110001,0);
while (!TFLG1); //Spin till you see the 1 on the flag
TFLG1 = 1; //Clear flag
TC0 = TC0 + 15625;
(void) SWLToggle(SWLGreen); //Toggle the green LED so I know it's working
(void) SEGHexLine2(count++);
}
}
/////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////
//Put a Hex symbol on a seg for the least sig.digit, decimal point opt.
int SEGNormal(byte Addr, byte Value, int DPOn)
{
//Trim address to 3 bits (only valid values are 0b000 to 0b111)
Addr &= 0b00000111;
//Trim value to 4 bits (only value values ar 0x00 to 0x0f)
Value &= 0b00001111;
//We'll use addr to build our command by adding additional bits
Addr |= 0b01011000; //A, Normal, Decode, Hex, No Data Coming
//Decimal point low (0) is on, not 1
if (DPOn) Value &= (~0x80);
else Value |= 0x80;
//Send command
PORTB = Addr;
SEGMCOMM;
SEGWSTROBE;
//Send data
PORTB = Value;
SEGMDATA;
SEGWSTROBE;
return 0;
}
//Put a custom symbol on a seg for the least sig.digit, decimal point opt.
int SEGCustom(byte Addr, byte Value, int DPOn)
{
//Trim address to 3 bits (only valid values are 0b000 to 0b111)
Addr &= 0b00000111;
//We'll use addr to build our command by adding additional bits
Addr |= 0b01111000; //A, Normal, no Decode, Hex, No Data Coming
//Decimal point low (0) is on, not 1
if (DPOn) Value &= (~0x80);
else Value |= 0x80;
//Send command
PORTB = Addr;
SEGMCOMM;
SEGWSTROBE;
//Send data
PORTB = Value;
SEGMDATA;
SEGWSTROBE;
return 0;
}
int SEGCodeB(byte Addr, byte Value)
{
//Trim address to 3 bits (only valid values are 0b000 to 0b111)
Addr &= 0b00000111;
//We'll use addr to build our command by adding additional bits
Addr |= 0b01010000; //B, Normal, Decode, Hex, No Data Coming
//Send command
PORTB = Addr;
SEGMCOMM;
SEGWSTROBE;
//Send data
PORTB = Value;
SEGMDATA;
SEGWSTROBE;
return 0;
}
int SEGClear(byte Addr)
{
(void) SEGCustom(Addr,0x00,0); //Custom char, no lights
return 0;
}
int SEGClearAll()
{
byte i = 0;
for (i = 0; i < 8; ++i) //Cycle through all 8 locations
(void) SEGClear(i);
return 0;
}
int SEGHexLine2(uint Value) //Display value in hex (<=FFFF)
{
byte i = 0; //Loop counter
for(i=0; i < 4; ++i)
(void) SEGNormal(7-i, Value >> (4*i), 0);
return 0;
}
int SEGDecLine2(uint Value) //Display value in decimal (<=9999)
{
byte i = 0; //Loop counter
if(Value > 9999) Value = 9999; // Or, we could just truncate
for(i = 0; i < 4; ++i)
{
(void) SEGNormal(7-i, Value%10, 0);
Value /= 10;
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// Interrupt Service Routines
/////////////////////////////////////////////////////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment