Skip to content

Instantly share code, notes, and snippets.

@MrTrick
Last active February 23, 2016 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrTrick/00f3abb1a95f813e76f5 to your computer and use it in GitHub Desktop.
Save MrTrick/00f3abb1a95f813e76f5 to your computer and use it in GitHub Desktop.
//----------------------------------------------------------------------------------------------
//
// starsII
//
// Ver 0.1 - written in sourceboost c for the 16F628
//
// Author : MrTrick
// Date : 2010-12-25
// License : GPLv2
//
// Description :
// * Sits in sleep mode until the button is pressed. (connected to MCLR)
// * Turns the background and foreground stars on.
// * Flickers the foreground stars occasionally.
// * Goes back to sleep after a period of time, or if the button is pressed again.
//
//----------------------------------------------------------------------------------------------
//#define TESTING
#include "system.h"
#include "rand.h"
#ifdef TESTING
#pragma DATA _CONFIG, _BODEN_OFF & _PWRTE_ON & _WDT_OFF & _LVP_OFF & _MCLRE_OFF & _INTRC_OSC_NOCLKOUT
#else
#pragma DATA _CONFIG, _BODEN_OFF & _PWRTE_ON & _WDT_OFF & _LVP_OFF & _MCLRE_ON & _INTRC_OSC_NOCLKOUT
#endif
#pragma CLOCK_FREQ 4000000
typedef unsigned char byte;
//The number of increments above 'normal' that a star increases in brightness
#define TWINKLE 70
//How many cycles will be run before the field switches off?
#define ON_CYCLES 28000 // About 10 minutes
//----------------------------------------------------------------------------------------------
// Variables
//----------------------------------------------------------------------------------------------
volatile bit background @PORTA.1;
volatile bit background_dir @TRISA.1;
volatile byte stars_port @PORTB;
volatile byte stars_dir @TRISB;
byte brightness[20];
byte i;
byte compare[4] @0x70; //Force into shared space
unsigned short cycles;
void init() {
// Random!
srand(1234);
// I/O - Initially all inputs
stars_dir = 0b11111111;
background_dir = 1;
// What should the device do?
#ifndef TESTING
// If it was RESET while in the middle of displaying, (PD will be set) switch it off.
if (test_bit(status, NOT_PD))
sleep();
// If it was RESET while in sleep, (PD will be cleared) start it.
else
clear_wdt(); //Set PD
#endif
//Run for a specific amount of time
cycles = ON_CYCLES;
}
void pulseBank()
{
//Minimum brightness per star
delay_100us(20);
//Switch off each star as the brightness reaches zero.
i=TWINKLE+1;
while(i--) {
if (!compare[0]) set_bit(stars_dir,7); compare[0]--;
if (!compare[1]) set_bit(stars_dir,6); compare[1]--;
if (!compare[2]) set_bit(stars_dir,5); compare[2]--;
if (!compare[3]) set_bit(stars_dir,4); compare[3]--;
}
}
void main() {
//Initialise, and decide whether to run the cycles or halt.
init();
//Run!
background_dir = 0;
background = 1;
while(--cycles)
{
#ifndef TESTING
//Sometimes twinkle a random star
byte x = rand() >> 8;
if (x < 20) brightness[x] = TWINKLE;
#endif
//Dim any brighter stars by one
asm {
movlw _brightness ; Load the start address into fsr
movwf _fsr
loop:
movf _indf, W ; Decrement the star if it is brighter
btfss _status, Z
decf _indf, F
incf _fsr, F ; Go to the next star, stop if done
movlw _brightness+20
xorwf _fsr, W
btfss _status, Z
goto loop
}
//Display Bank 0's stars
compare[0] = brightness[0];
compare[1] = brightness[1];
compare[2] = brightness[2];
compare[3] = brightness[3];
stars_port = 0b11110000;
stars_dir = 0b00001101; // 7654 out, 1 in.
pulseBank();
//Display Bank 1's stars
compare[0] = brightness[4];
compare[1] = brightness[5];
compare[2] = brightness[6];
compare[3] = brightness[7];
stars_port = 0b00001000;
stars_dir = 0b00000111; // 3 out, 7654 in.
pulseBank();
//Display Bank 2's stars
compare[0] = brightness[8];
compare[1] = brightness[9];
compare[2] = brightness[10];
compare[3] = brightness[11];
stars_port = 0b00000100;
stars_dir = 0b00001011; // 2 out, 7654 in.
pulseBank();
//Display Bank 3's stars
compare[0] = brightness[12];
compare[1] = brightness[13];
compare[2] = brightness[14];
compare[3] = brightness[15];
stars_port = 0b00000010;
stars_dir = 0b00001101; // 1 out, 7654 in.
pulseBank();
//Display Bank 4's stars
compare[0] = brightness[16];
compare[1] = brightness[17];
compare[2] = brightness[18];
compare[3] = brightness[19];
stars_port = 0b00000001;
stars_dir = 0b00001110; // 0 out, 7654 in.
pulseBank();
}
//Switch off the stars and go to sleep. (until reset again) Goodnight!
stars_dir = 0b11111111;
background_dir = 1;
sleep();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment