Skip to content

Commit 480adc8

Browse files
authored
added src files
ADCrand595 is an example using ATtiny10 as random number generator with Seg7 debugger attached.
1 parent cc92806 commit 480adc8

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

ADCrand595.ino

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <util/delay.h>
2+
#include "Seg7.h"
3+
4+
#define ADC0 0
5+
#define ADC1 1
6+
#define ADC2 2
7+
#define ADC3 3
8+
9+
10+
//Pin connected to ST_CP of 74HC595 = 12 = RCLK
11+
#define latchPin B2
12+
//Pin connected to SH_CP of 74HC595 = 11 = SRCLK
13+
#define clockPin B0
14+
////Pin connected to DS of 74HC595 = 14 = SER
15+
#define dataPin B1
16+
17+
void setup() {
18+
19+
ADMUX |= ADC3; // <<MUX0; // choose ADC
20+
ADCSRA = 1<<ADEN | 3<<ADPS0; // Enable ADC, 125kHz clock
21+
22+
Seg7_init(); // this will set B0, B1, B2 to OUTPUT for both the Seg7 and the 74HC595
23+
24+
DIDR0 = 0x0F;
25+
26+
Seg7_clear( 0 ); // Cleat the Seg7 display starting at position 0
27+
Seg7_hex( 0xA3, 1 ); // write the value 0xA3 to the display - we will be displaying the value of ADC3
28+
dWrite( MAX7219_Clk, LOW ); // start the 595 clock low
29+
}
30+
31+
void loop() {
32+
33+
uint8_t x, d = 0;
34+
dWrite(latchPin, LOW); // keep the 595 latch/load pin LOW
35+
36+
ADCSRA = ADCSRA | 1<<ADSC; // Start ADC
37+
while (ADCSRA & 1<<ADSC); // Wait while conversion in progress
38+
_delay_ms( 2 );
39+
40+
d = ( ADCL ) & 1; // we are only interested in the lowest order bit of ADC3 for use as a random 1 or 0
41+
42+
Seg7_hex( d, 5 ); // display the value of d at position 5
43+
_delay_ms( 250 ); // update every 1/4 second
44+
45+
}

Seg7.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef __Seg_7_H__
2+
#define __Seg_7_H__
3+
4+
// #include <inttypes.h>
5+
// #include <Arduino.h>
6+
7+
#define delay( x ) _delay_ms( x )
8+
9+
#define MAX7219_Din B1
10+
#define MAX7219_CS B0
11+
#define MAX7219_Clk B2
12+
13+
#define NUM_DIGITS 8
14+
15+
16+
#define HIGH 1
17+
#define LOW 0
18+
#define delay( x ) _delay_ms( x )
19+
20+
#define DecodeMODE_REG 0x09
21+
#define BrightNESS_REG 0x0A
22+
#define ScanlIMIT_REG 0x0B
23+
#define ShutDown_REG 0x0C
24+
#define DisplayTEST_REG 0x0F
25+
26+
#endif

Seg7_Tiny10.ino

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include "Seg7.h"
2+
3+
uint8_t msk; // decode mask
4+
5+
void Seg7_init() {
6+
pMode( MAX7219_Din, OUTPUT);
7+
pMode( MAX7219_CS, OUTPUT);
8+
pMode( MAX7219_Clk, OUTPUT);
9+
10+
output(DisplayTEST_REG, 0x0); // turn TEST mode OFF
11+
output(ShutDown_REG, 0x1); // Start UP MAX7219
12+
output(BrightNESS_REG, 0 ); // Brightness to minimum
13+
output(DecodeMODE_REG, 0xFF); // all digits to BCD mode
14+
output(ScanlIMIT_REG, NUM_DIGITS-1); // Display all digits
15+
16+
}
17+
18+
void Seg7_dec( uint16_t nm, uint8_t p ) {
19+
20+
uint8_t x = 0;
21+
char stg[5];
22+
stg[ 0 ] = ( nm/1000 ) | 0x30; nm = nm % 1000;
23+
stg[ 1 ] = ( nm/100 ) | 0x30; nm = nm % 100;
24+
stg[ 2 ] = ( nm/10 ) | 0x30; nm = nm % 10;
25+
stg[ 3 ] = nm;
26+
stg[ 4 ] = 0;
27+
28+
while( stg[x] ) {
29+
30+
output( p, stg[x] );
31+
32+
x++; p--;
33+
}
34+
}
35+
36+
void Seg7_hex( uint16_t nm, uint8_t p ) {
37+
uint8_t Seg7font[6] = { 0x77, 0x1F, 0x4E, 0x3D, 0x4F, 0x47 };
38+
uint8_t x = 0;
39+
uint8_t stg[5];
40+
if( nm < 256 ) {
41+
stg[ 0 ] = ((nm & 0xF0) >> 4) | 0x30;
42+
stg[ 1 ] = ( nm & 0xF ) | 0x30;
43+
stg[ 2 ] = 0;
44+
}
45+
else {
46+
stg[ 0 ] = ((nm & 0xF000) >> 12) | 0x30;
47+
stg[ 1 ] = ((nm & 0xF00 ) >> 8 ) | 0x30;
48+
stg[ 2 ] = ((nm & 0xF0 ) >> 4 ) | 0x30;
49+
stg[ 3 ] = (nm & 0xF) | 0x30;
50+
stg[ 4 ] = 0;
51+
}
52+
53+
while( stg[x] ) { stg[x] = stg[x] & 0xF;
54+
if( stg[x] > 9 ) {
55+
msk &= ~( 1<<( 7-p ) );
56+
output( DecodeMODE_REG, msk );
57+
output( 8-p, Seg7font[ stg[x]-10 ] );
58+
}
59+
else {
60+
output( 8-p, stg[x] );
61+
}
62+
63+
x++; p++;
64+
}
65+
}
66+
67+
/**** the Arduino shiftOut function is not available in the Core10 package **/
68+
void shift_out( uint8_t dta )
69+
{
70+
for( uint8_t x = 0; x < 8; x++ )
71+
{
72+
dWrite( MAX7219_Din, dta & 0x80 ); delay( 1 );
73+
dWrite( MAX7219_Clk, HIGH ); delay( 1 );
74+
dWrite( MAX7219_Clk, LOW );
75+
dta = dta << 1;
76+
}
77+
}
78+
79+
void Seg7_clear( uint8_t p ) {
80+
81+
int x;
82+
output(DecodeMODE_REG, 0xFF ); msk = 0xFF;
83+
for( x=8-p; x>0; x-- ) { output( x, 0xF ); }
84+
85+
}
86+
87+
void Seg7_brightness( uint8_t b ) {
88+
output( BrightNESS_REG, b );
89+
}
90+
91+
void output( uint8_t address, uint8_t data) {
92+
93+
dWrite( MAX7219_CS, LOW );
94+
shift_out( address);
95+
shift_out( data );
96+
dWrite( MAX7219_CS, HIGH );
97+
98+
}
99+
100+
101+

0 commit comments

Comments
 (0)