/*
byte numDigits = 4;
byte digitPins[] = {2, 3, 4, 5};
byte numSegments = 8;
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
byte tabDeco7seg[]={0x3f,0x06,0x5b,0x4F,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
byte digits[4] = {1,7,10,15};
void setup() {
Serial.begin(115200);
Serial.println("debut");
pinMode(A3,OUTPUT);
digitalWrite(A3, LOW);
// Initialisation des broches pour les digits
for (byte i = 0; i < numDigits; i++) {
pinMode(digitPins[i], OUTPUT);
digitalWrite(digitPins[i], HIGH);
}
// Initialisation des broches pour les segments
for (byte i = 0; i < numSegments; i++) {
pinMode(segmentPins[i], OUTPUT);
digitalWrite(segmentPins[i], LOW);
}
setupTimer2();
}
void setDigit(unsigned char nDigitActif,unsigned char nSegsActif)
{
//1.activer la broche A3 pour début de mesure de la durée d'exécution: TIC
PORTC |= 0b01 << 3;
//2.régler toutes les cathodes inactives
PORTD = 0b00111100; // supprime la valeur précédente des segments de poid faible
//3.régler les segments (poids faible), en maintenant toutes les cathodes inactives
PORTD |= (nSegsActif & 0x3) << 6;
//PORTB = ((nSegsActif >> 2) & 0b11);
//4.régler les segments (poids fort)
PORTB &= ~((nSegsActif >> 2) & 0x3F) & ((nSegsActif >> 2) & 0x3F);
PORTB |= ((nSegsActif >> 2) & 0x3F) << 0;
//5.régler la cathode active
//PORTD &= ~((nDigitActif & 0b1111) << 2);
PORTD &= ~((0b1)<< (2 + nDigitActif));
//PORTD = 0b0100;
//6.désactiver la broche A3 pour fin de mesure de la durée d'exécution:TOC
PORTC &= ~(0b1<<3);
}
void setDigitHAL(unsigned char nDigitActif,unsigned char nSegsActif)
{
PORTC |= 0b01 << 3;
// Désactiver tous les digits avant de changer les segments
for (byte i = 0; i < numDigits; i++) {
digitalWrite(digitPins[i], HIGH); // État inactif (HIGH pour Anode Commune)
}
// Piloter les segments en utilisant les valeurs des bits de nSegsActif
for (byte i = 0; i < numSegments; i++) {
byte segmentState = (nSegsActif >> i) & 0x01;
digitalWrite(segmentPins[i], segmentState);
}
// Activer le bon digit
digitalWrite(digitPins[nDigitActif], LOW); // État actif (LOW pour Anode Commune)
PORTC = PORTC & ~(1<<3);
}
void setNumber(unsigned int val)
{
cli(); // disable all interrupts
if(val > 9999)
{
digits[0] = 14;
digits[1] = 14;
digits[2] = 14;
digits[3] = 14;
}
else
{
for(byte i = numDigits; i > 0; i--)
{
digits[(i-1)] = val % 10;
val = val / 10;
}
}
sei(); // enable all interrupts
}
void setupTimer2(){
cli(); // disable all interrupts
TCCR2A = (1<<WGM21)|(0<<WGM20); // Mode CTC
TIMSK2 = (1<<OCIE2A); // Local interruption OCIE2A
TCCR2B = (0<<WGM22)|(1<<CS22)|(1<<CS21); // prediviser /256
OCR2A = 250; // 250*256*1/16000000 = 4ms
sei(); // enable all interrupts
}
/*void loop() {
static unsigned long timer = millis();
static int deciSeconds = 0;
uint16_t val = analogRead(A0);
if (millis() - timer >= 100) {
timer += 100;
deciSeconds++; // 100 milliSeconds is equal to 1 deciSecond
if (deciSeconds == 10000) { // Reset to 0 after counting for 1000 seconds.
deciSeconds=0;
}
//digitalWrite(digitPins[2], HIGH);
//digitalWrite(segmentPins[5], LOW);
//digitalWrite(digitPins[0], HIGH);
//digitalWrite(segmentPins[0], LOW);
//digitalWrite(segmentPins[4], LOW);
setDigit()
}
sevseg.refreshDisplay(); // Must run repeatedly
}*/
/*void loop() {
setDigit(0,0x10);
setDigit(1,0x20);
setDigit(2,0x40);
setDigit(3,0x80);
}*/
/*void loop() {
setDigit(0,1);
setDigit(1,2);
setDigit(2,3);
setDigit(3,4);
}
byte dots = 4; // Par défaut, tous les points décimaux activés
void tache1() {
static uint16_t val = 7895;
setNumber(val);
val++;
if (val >= 9999){
val = 0;
}
delay(25);
}
void tache2() {
// Tâche 2 : Affichage multiplexé
static byte currentDigit = 0;
byte digitValue = digits[currentDigit];
byte segsWithDot = tabDeco7seg[digitValue] | (((dots >> currentDigit) & 0x01) << 7);
setDigit(currentDigit, segsWithDot);
//setDigitHAL(currentDigit, segsWithDot);
currentDigit++;
if (currentDigit == numDigits) {
currentDigit = 0; // Revenir au premier digit après avoir balayé tous les digits
}
}
//appelée toutes les 4ms:
ISR(TIMER2_COMPA_vect){ // timer compare interrupt service routine
tache2();
}
void loop() {
unsigned int periodiciteTache1 = 100; // 100ms entre chaque incrémentation de la valeur à afficher
static unsigned long timerTache1 = millis();
if (millis() - timerTache1 >= periodiciteTache1) {
timerTache1 += periodiciteTache1;
tache1();
}
}
*/
#include "lib7seg.h"
unsigned int datain[4];
unsigned long updatevalue = 0;
unsigned long updatedisplayvalue = 0;
byte displayIndex = 0;
void setupTimer2(){
cli(); // disable all interrupts
TCCR2A = (1<<WGM21)|(0<<WGM20); // Mode CTC
TIMSK2 = (1<<OCIE2A); // Local interruption OCIE2A
TCCR2B = (0<<WGM22)|(1<<CS22)|(1<<CS21); // prediviser /256
OCR2A = 250; // 250*256*1/16000000 = 4ms
sei(); // enable all interrupts
}
//appelée toutes les 4ms:
ISR(TIMER2_COMPA_vect){ // timer compare interrupt service routine
refresh();
}
void setup() {
Serial.begin(115200);
Serial.println("debut");
pinMode(A3, OUTPUT);
digitalWrite(A3, LOW);
setup7seg(); // Initialisation spécifique à l'afficheur 7 segments
setupTimer2(); // Reste dans le fichier principal
}
void tache_1()
{
datain[0] = analogRead(A0);
datain[1] = analogRead(A1);
datain[2] = analogRead(A2);
datain[3] = analogRead(A4);
for (int i = 0; i < 4; i++) {
Serial.print(datain[i]);
Serial.print(" ");
}
Serial.println();
}
void tache2()
{
displayIndex = (displayIndex + 1) % 4;
setDots(1 << displayIndex);
}
void tache_3()
{
setNumber(datain[displayIndex]);
}
/*
void loop() {
unsigned int periodiciteTache1 = 100; // 100ms entre chaque incrémentation de la valeur à afficher
static unsigned long timerTache1 = millis();
if (millis() - timerTache1 >= periodiciteTache1) {
timerTache1 += periodiciteTache1;
tache1();
}
}*/
void loop()
{
if (millis() - updatevalue >= 100) {
updatevalue = millis();
tache_1();
for (int i = 0; i < 4; i++) {
Serial.print(datain[i]);
if (i < 3) Serial.print(" ");
}
Serial.println();
}
if (millis() - updatedisplayvalue >= 2000) {
updatedisplayvalue = millis();
tache2();
}
//tache_3();
setNumber(datain[displayIndex]);
}
/// END ///