// Pin Definitions
#define MAX7219_LOAD_PIN PB0
#define MAX7219_CLK_PIN PB1
#define MAX7219_DATA_PIN PB2
#define TEMP_SET_SWITCH PC0
#define TEMP_TOGGLE_SWITCH1 PA0
#define TEMP_TOGGLE_SWITCH2 PA1
#define MOTOR_SPEED_SWITCH1 PA2
#define MOTOR_SPEED_SWITCH2 PA3
#define BUZZER_PIN PA4
#define LED1_PIN PA5
#define LED2_PIN PA6
#define LED3_PIN PA7
#define MOTOR_SEGMENTS_PIN PC1
// Global Variables
volatile uint8_t temperatureSet = 0;
volatile uint8_t temperatureReady = 0;
volatile uint8_t motorSpeed = 0;
// Function Declarations
void initGPIO();
void initADC();
void initSPI();
void initInterrupts();
void displayNumber(uint8_t number);
void buzzerOn();
void buzzerOff();
void motorSpeedControl();
int main() {
// Initialize
initGPIO();
initADC();
initSPI();
initInterrupts();
// Turn on LED1 to indicate power
PORTA |= (1 << LED1_PIN);
// Main Loop
while (1) {
// Read temperature sensor
ADCSRA |= (1 << ADSC); // Start ADC conversion
while (ADCSRA & (1 << ADSC)); // Wait for conversion to complete
uint16_t adcValue = ADC;
// Calculate temperature
float voltage = (adcValue * 5.0) / 1024.0; // ADC reference voltage is 5V
float resistance = (5.0 * 10000.0 / voltage) - 10000.0; // NTC resistance
float temperature = 1 / (1 / 298.15 + log(resistance / 100000.0) / 3950.0) - 273.15; // NTC temperature equation
// Check if temperature set switch is on
if (temperatureSet) {
// Set the temperature threshold (e.g., 25°C)
if (temperature >= temperatureSet) {
// Temperature threshold reached, turn on LED3
PORTA |= (1 << LED3_PIN);
temperatureReady = 1;
} else {
// Temperature below threshold, turn off LED3
PORTA &= ~(1 << LED3_PIN);
temperatureReady = 0;
}
}
// Check if temperature toggle switches are on
if ((PINA & (1 << TEMP_TOGGLE_SWITCH1)) && (PINA & (1 << TEMP_TOGGLE_SWITCH2))) {
// Both toggle switches are on, turn on heater
// ...
} else {
// Either one or both toggle switches are off, turn off heater
// ...
}
// Update motor speed based on the switches
motorSpeedControl();
// Display motor speed on 2811AS seven-segment display
displayNumber(motorSpeed);
_delay_ms(100); // Delay for stability
}
return 0;
}
void initGPIO() {
// Set pin directions
DDRC |= (1 << MAX7219_LOAD_PIN) | (1 << MAX7219_CLK_PIN) | (1 << MAX7219_DATA_PIN);
DDRA &= ~(1 << TEMP_SET_SWITCH) & ~(1 << TEMP_TOGGLE_SWITCH1) & ~(1 << TEMP_TOGGLE_SWITCH2) & ~(1 << MOTOR_SPEED_SWITCH1) & ~(1 << MOTOR_SPEED_SWITCH2);
DDRA |= (1 << BUZZER_PIN) | (1 << LED1_PIN) | (1 << LED2_PIN) | (1 << LED3_PIN);
DDRC |= (1 << MOTOR_SEGMENTS_PIN);
Welcome to the forum
Which Arduino board are you compiling this for ?
I am compiling for: ATTINY1634R
This code is not compatible for Attiny
Correction:
This code is not compatible with Attiny or with your attiny package installed.
What the package you used for Attiny 1634 ?
can i use it with atmega128
Arduino ide
Select the atmega128 in IDE and try to compile.
Bare Arduino IDE probably is not enough for this.
To work with Attiny-group controllers is strictly recommended to install a dedicated package, for example AttinyCore:
It would be better to use the code exactly with mcu to which it programmed.
Especially if you don't have a much expediency with micro-controllers.
Where did you find this code? Does the project have any description? Maybe it says what chip it is for?
No, the Megas and the Tinys are very different
When I try to compile the code I get an error for _delay_ms
If I comment that out it wont compile because of the missing bracket } at the end but nothing about PORTA
This is the complete code:
// ATTINY1634R Motor_Temperature Control Code
// Include required libraries
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <math.h>
// Pin Definitions
#define MAX7219_LOAD_PIN PB0
#define MAX7219_CLK_PIN PB1
#define MAX7219_DATA_PIN PB2
#define TEMP_SET_SWITCH PC0
#define TEMP_TOGGLE_SWITCH1 PA0
#define TEMP_TOGGLE_SWITCH2 PA1
#define MOTOR_SPEED_SWITCH1 PA2
#define MOTOR_SPEED_SWITCH2 PA3
#define BUZZER_PIN PA4
#define LED1_PIN PA5
#define LED2_PIN PA6
#define LED3_PIN PA7
#define MOTOR_SEGMENTS_PIN PC1
// Global Variables
volatile uint8_t temperatureSet = 0;
volatile uint8_t temperatureReady = 0;
volatile uint8_t motorSpeed = 0;
// Function Declarations
void initGPIO();
void initADC();
void initSPI();
void initInterrupts();
void displayNumber(uint8_t number);
void buzzerOn();
void buzzerOff();
void motorSpeedControl();
int main() {
// Initialize
initGPIO();
initADC();
initSPI();
initInterrupts();
// Turn on LED1 to indicate power
PORTA |= (1 << LED1_PIN);
// Main Loop
while (1) {
// Read temperature sensor
ADCSRA |= (1 << ADSC); // Start ADC conversion
while (ADCSRA & (1 << ADSC)); // Wait for conversion to complete
uint16_t adcValue = ADC;
// Calculate temperature
float voltage = (adcValue * 5.0) / 1024.0; // ADC reference voltage is 5V
float resistance = (5.0 * 10000.0 / voltage) - 10000.0; // NTC resistance
float temperature = 1 / (1 / 298.15 + log(resistance / 100000.0) / 3950.0) - 273.15; // NTC temperature equation
// Check if temperature set switch is on
if (temperatureSet) {
// Set the temperature threshold (e.g., 25°C)
if (temperature >= temperatureSet) {
// Temperature threshold reached, turn on LED3
PORTA |= (1 << LED3_PIN);
temperatureReady = 1;
} else {
// Temperature below threshold, turn off LED3
PORTA &= ~(1 << LED3_PIN);
temperatureReady = 0;
}
}
// Check if temperature toggle switches are on
if ((PINA & (1 << TEMP_TOGGLE_SWITCH1)) && (PINA & (1 << TEMP_TOGGLE_SWITCH2))) {
// Both toggle switches are on, turn on heater
// ...
} else {
// Either one or both toggle switches are off, turn off heater
// ...
}
// Update motor speed based on the switches
motorSpeedControl();
// Display motor speed on 2811AS seven-segment display
displayNumber(motorSpeed);
_delay_ms(100); // Delay for stability
}
return 0;
}
void initGPIO() {
// Set pin directions
DDRC |= (1 << MAX7219_LOAD_PIN) | (1 << MAX7219_CLK_PIN) | (1 << MAX7219_DATA_PIN);
DDRA &= ~(1 << TEMP_SET_SWITCH) & ~(1 << TEMP_TOGGLE_SWITCH1) & ~(1 << TEMP_TOGGLE_SWITCH2) & ~(1 << MOTOR_SPEED_SWITCH1) & ~(1 << MOTOR_SPEED_SWITCH2);
DDRA |= (1 << BUZZER_PIN) | (1 << LED1_PIN) | (1 << LED2_PIN) | (1 << LED3_PIN);
DDRC |= (1 << MOTOR_SEGMENTS_PIN);
// Enable internal pull-up resistors for switches
PORTA |= (1 << TEMP_SET_SWITCH) | (1 << TEMP_TOGGLE_SWITCH1) | (1 << TEMP_TOGGLE_SWITCH2) | (1 << MOTOR_SPEED_SWITCH1) | (1 << MOTOR_SPEED_SWITCH2);
}
void initADC() {
// Set voltage reference to AVCC
ADMUX |= (1 << REFS0);
// Enable ADC, enable ADC interrupts, and set ADC prescaler to 128
ADCSRA |= (1 << ADEN) | (1 << ADIE) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
}
void initSPI() {
// Set SPI pins as output
DDRB |= (1 << MAX7219_LOAD_PIN) | (1 << MAX7219_CLK_PIN) | (1 << MAX7219_DATA_PIN);
// Enable SPI, set as master, and set clock rate to fck/16
SPCR |= (1 << SPE) | (1 << MSTR) | (1 << SPR0);
}
void initInterrupts() {
// Enable external interrupts on INT0 (PA0)
EIMSK |= (1 << INT0);
// Trigger interrupt on any logical change
EICRA |= (1 << ISC00);
EICRA &= ~(1 << ISC01);
// Enable global interrupts
sei();
}
void displayNumber(uint8_t number) {
// Send data to MAX7219 using SPI
PORTC &= ~(1 << MAX7219_LOAD_PIN); // Enable load pin
// Send command for the first segment
SPDR = 1; // Register address
while (!(SPSR & (1 << SPIF))); // Wait for transmission to complete
SPDR = number % 10; // Data for the first segment
while (!(SPSR & (1 << SPIF))); // Wait for transmission to complete
// Send command for the second segment
SPDR = 2; // Register address
while (!(SPSR & (1 << SPIF))); // Wait for transmission to complete
SPDR = number / 10; // Data for the second segment
while (!(SPSR & (1 << SPIF))); // Wait for transmission to complete
PORTC |= (1 << MAX7219_LOAD_PIN); // Disable load pin
}
void buzzerOn() {
PORTA |= (1 << BUZZER_PIN);
}
void buzzerOff() {
PORTA &= ~(1 << BUZZER_PIN);
}
void motorSpeedControl() {
// Read motor speed switches
uint8_t speedSwitch1 = (PINA & (1 << MOTOR_SPEED_SWITCH1)) ? 1 : 0;
uint8_t speedSwitch2 = (PINA & (1 << MOTOR_SPEED_SWITCH2)) ? 1 : 0;
// Determine motor speed based on switch states
if (!speedSwitch1 && !speedSwitch2) {
motorSpeed = 0; // Speed 0
} else if (!speedSwitch1 && speedSwitch2) {
motorSpeed = 1; // Speed 1
} else if (speedSwitch1 && !speedSwitch2) {
motorSpeed = 2; // Speed 2
} else if (speedSwitch1 && speedSwitch2) {
motorSpeed = 3; // Speed 3
}
}
// Interrupt Service Routine for temperature set switch
ISR(INT0_vect) {
// Toggle temperature set
temperatureSet = !temperatureSet;
// Turn on LED2 to indicate temperature set
PORTA ^= (1 << LED2_PIN);
// Generate a short beep
buzzerOn();
_delay_ms(100);
buzzerOff();
}
// Interrupt Service Routine for ADC conversion complete
ISR(ADC_vect) {
// ADC conversion complete
// Handle any necessary actions here
}
Try to install package from the post #8
What procedure are you using to compile?
When I use ATtinyCore (as people are suggesting), I get a bunch of errors related to to your SPI initialization - that's because the 1634 doesn't have an "SPI" peripheral; it has a USI that can be used for SPI. (which is weird; usually the chips that have USI are deficient in other peripherals as well, being really "bare bones." But the 1634 has TWI and 2(!) UARTs...)
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.