hello this code was write for attiny13 and when i used arduino uno as isp to program a attiny85 with this code it's not working. It s a dice and at the end the attiny must "go to sleep" but nothing works the dice show impossible lottery. What s wrong ?
please help me.
here the code :
//------------------------------
// De electronique a ATtiny13
// (C) 2009, Pierre-Yves Rochat
//------------------------------
#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
// Entree-sorties
//---------------
#define BitUn PORTB0
#define BitDeux PORTB2
#define BitTrois PORTB4
#define BitSix PORTB1
#define BitPoussoir PORTB3
#define Un ~(1<<BitUn)
#define Deux ~(1<<BitDeux)
#define Trois ~((1<<BitUn)|(1<<BitTrois))
#define Quatre ~((1<<BitDeux)|(1<<BitTrois))
#define Cinq ~((1<<BitUn)|(1<<BitDeux)|(1<<BitTrois))
#define Six ~((1<<BitDeux)|(1<<BitTrois)|(1<<BitSix))
#define Zero ~0
// procedures pour endormir le processeur
ISR (PCINT0_vect) { // l'interruption termine le mode sleep
sleep_disable();
}
void Sommeil() {
PORTB=Zero;
PCMSK|=(1<<BitPoussoir); // Pin Change sur PB3
GIMSK|=(1<<PCIE); // Pin Change Interupt Inable
sei();
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // mieux: 3v a 2.5v en 10 sec.
sleep_enable();
sleep_cpu(); // on passe en mode sleep.
// l'interruption le reveillera
}
void Hello() {
volatile unsigned int i, j;
PORTB=~(1<<BitUn);
for(i=0; i<10000; i++);
for(j=0; j<5; j++){
PORTB=~(1<<BitDeux);
for(i=0; i<3000; i++);
PORTB=~(1<<BitSix);
for(i=0; i<3000; i++);
PORTB=~(1<<BitTrois);
for(i=0; i<3000; i++);
}
}
// Gestion du poussoir
//--------------------
void Attente(unsigned char Valeur) {
volatile unsigned int i;
#define Delai 300 // a changer par 300 pour aller vite !
unsigned int Minuterie=0;
PORTB=Valeur; // a remplacer par Zero pour economier la pile
// PORTB=Zero;
for(i=0; i<Delai; i++) {
// rien, juste pour passer le temps
}
while(PINB&(1<<BitPoussoir)) { // attente que le poussoir soit presse
PORTB=Valeur;
for(i=0; i<Delai; i++);
Minuterie++;
if (Minuterie==1000) {
Sommeil();
Minuterie=0;
Hello();
Valeur=Un;
}
}
}
// programme principal
//--------------------
int main(){
DDRB=(1<<BitUn)|(1<<BitDeux)|(1<<BitTrois)|(1<<BitSix); // sorties
PORTB=(1<<BitPoussoir); // entree, avec Pull-up
Hello();
while(1){ // boucle sans fin
Attente(Un);
Attente(Deux);
Attente(Trois);
Attente(Quatre);
Attente(Cinq);
Attente(Six);
}
}
What is wrong? Well, first of all your questioning.
What does it do (it can't be "nothing" as you say you're seeing an "impossible lottery" whatever that may be), and what is it supposed to do?
What is your hardware?
How is it wired?
You are using a lot of pins on the Attiny85 ( if I understand correctly, somehow you are using 7 leds and a button which must be close to the limit for an 8 pin device. ! ).
with the program up or this i have try both and same result.
//------------------------------
// Dé électronique à ATtiny13
// (C) 2009, Pierre-Yves Rochat
//------------------------------
// Version "EPFL", août 2010
#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
// Nombre de seconde avant le sommeil
#define DelaiSommeil 10
// Paramètre du "Hello":
// Nombre de fois le motif (0 à 7)
#define NbHello 5
// Durée des motifs (autour de 30)
#define DureeHello 30
// Ports utilisée pour les groupes de LED:
#define PortLeds PORTB
#define BitUn PORTB0
#define BitDeux PORTB2
#define BitTrois PORTB4
#define BitSix PORTB1
// Port utilisé pour le poussoir
#define PinPoussoir PINB
#define BitPoussoir PORTB3
// Groupements pour obtenir les chiffres:
#define Un ~(1<<BitUn)
#define Deux ~(1<<BitDeux)
#define Trois ~((1<<BitUn)|(1<<BitTrois))
#define Quatre ~((1<<BitDeux)|(1<<BitTrois))
#define Cinq ~((1<<BitUn)|(1<<BitDeux)|(1<<BitTrois))
#define Six ~((1<<BitDeux)|(1<<BitTrois)|(1<<BitSix))
// autres combinaisons:
#define Zero ~0
#define Sept ~((1<<BitUn)|(1<<BitDeux)|(1<<BitTrois)|(1<<BitSix))
#define Special ~(1<<BitSix)
// Procédure pour afficher la valeur;
void Affiche (char valeur) {
PortLeds=Zero;
if (valeur==1) PortLeds=Un;
if (valeur==2) PortLeds=Deux;
if (valeur==3) PortLeds=Trois;
if (valeur==4) PortLeds=Quatre;
if (valeur==5) PortLeds=Cinq;
if (valeur==6) PortLeds=Six;
}
// Procédures pour endormir le processeur
ISR (PCINT0_vect) { // l'interruption termine le mode sleep
sleep_disable();
}
void Sommeil() {
PortLeds= Zero;
PCMSK|=(1<<BitPoussoir); // Pin Change sur PB3
GIMSK|=(1<<PCIE); // Pin Change Interupt Inable
sei();
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // mieux: 3v à 2.5v en 10 sec.
sleep_enable();
sleep_cpu(); // on passe en mode sleep.
// l'interruption le réveillera
}
// Animation de réveil:
void Hello() {
volatile unsigned int i, j;
PortLeds=~(1<<BitUn);
for(i=0; i<10000; i++);
for(j=0; j<NbHello; j++){
PortLeds=~(1<<BitDeux);
for(i=0; i<(DureeHello*100); i++);
PortLeds=~(1<<BitSix);
for(i=0; i<(DureeHello*100); i++);
PortLeds=~(1<<BitTrois);
for(i=0; i<(DureeHello*100); i++);
}
}
// Effet lors de l'arrrêt
void Effet (char valeur) {
volatile unsigned int i;
PortLeds=Sept;
for(i=0; i<3000; i++); // suspens
Affiche (valeur);
}
// Gestion du poussoir, avec sommeil
void GestionPoussoir(unsigned char valeur) {
volatile unsigned int i;
unsigned int Minuterie=0;
Affiche(valeur); // on peut remplacer par Zero pour économier la pile
for(i=0; i<300; i++); // pour attendre un peu
while(PinPoussoir&(1<<BitPoussoir)) { // attente que le poussoir soit pressé
for(i=0; i<(DelaiSommeil*30); i++);
Minuterie++;
if (Minuterie==1000) {
Sommeil();
Minuterie=0;
Hello();
}
Affiche(valeur);
}
}
// programme principal
//--------------------
int main(){
DDRB=(1<<BitUn)|(1<<BitDeux)|(1<<BitTrois)|(1<<BitSix); // sorties
PORTB=(1<<BitPoussoir); // entrée, avec Pull-up
Hello();
while(1){ // boucle sans fin
Affiche(1);
GestionPoussoir(1);
Affiche(2);
GestionPoussoir(2);
Affiche(3);
GestionPoussoir(3);
Affiche(4);
GestionPoussoir(4);
Affiche(5);
GestionPoussoir(5);
Affiche(6);
GestionPoussoir(6);
}
}
[code]
Pierre-Yves Rochat est professeur à l'EPFL et on doit pouvoir trouver ses coordonnées sans trop de difficultés pour lui poser une question correctement formulée.
I think that the controller must run at a very low clock frequency, else the delays have no effect. Then the first posted code should work as expected.
Bianco:
Pierre-Yves Rochat est professeur à l'EPFL et on doit pouvoir trouver ses coordonnées sans trop de difficultés pour lui poser une question correctement formulée.
yes i have contacted and he gave me this code.
But i don't want to disturbing him more it was already nice that he send to me the code.
But he said that it will be ok so i don't know why is not working. I 'm programming the attiny85 with arduino uno as isp with the ide program for arduino. Perhaps the problem is here. how can i programming my attiny85 with different way ?
Using "this" would cause you more trouble. You don't know what software it uses and might need to reflash it. Making "this" work is a project of its own.
If your uno programs your ATtiny correctly without using a sleep function, don't switch.