Вс апр 10, 2022 19:11:44
Вс апр 10, 2022 19:18:33
Вс апр 10, 2022 19:22:19
Вт май 17, 2022 15:08:27
Вт май 17, 2022 16:26:43
Вт май 17, 2022 17:49:21
Вт май 17, 2022 18:06:16
Вт май 17, 2022 18:12:46
Вт май 17, 2022 18:24:57
Вт май 17, 2022 23:04:34
Пт май 20, 2022 08:59:07
Пт май 20, 2022 16:17:24
Пн июн 05, 2023 12:32:19
void SPI_MasterInit(void) {
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); /* Enable SPI, Master, set clock rate fck/16 */
}
void SPI_MasterTransmit(uint8_t cData) {
SPDR = cData; /* Start transmission */
while(!(SPSR & (1<<SPIF))); /* Wait for transmission complete */
}void SPITransmitFreq(uint8_t Cnt, double F) { //F- значение частоты, Cnt - номер генератора
dFi.w = F*167.77216; //частота передаваемая через SPI, какое-то фиксированное число 167.77216
PORTB &= ~_BV(PB0);
SPI_MasterTransmit(Cnt & 0x03);//0x03-маска
SPI_MasterTransmit(dFi.b[0]);//флаги передачи
SPI_MasterTransmit(dFi.b[1]);//флаги передачи
SPI_MasterTransmit(dFi.b[2]);//флаги передачи
PORTB |= _BV(PB0);
}
#include <tiny2313.h>
#include <math.h>
#include <io.h>
#define F_CPU (8000000)
#define VFG_TIMER_MAX (65535)
#define VFG_DDR DDRB
#define VFG_PORT PORTB
#define CS PORTD3 // Chip select
#define DO PORTB6 // MISO or Data Out
#define USCK PORTB7 // Clock
unsigned int fG;
unsigned int fG1;
unsigned int fG2;
unsigned int fG3;
unsigned char nG;
unsigned int N[]={1,8,64,256,1024};
volatile char reqID = 0; // This is for the first byte we receive, which is intended to be the request identifier
volatile unsigned char index = 0; // this is to send back the right element in the array
//***********************************************USI************************************************
void SpiSlaveInit() {
#asm("cli")
USICR = ((1<<USIWM0)|(1<<USICS1)); // Activate 3- Wire Mode and use of external clock but NOT the interrupt at the Counter overflow (USIOIE)
PORTB |= 1<<CS; // Activate Pull-Up resistor on PB0
PCMSK|=1<<CS; // Active Interrupt on PB1
GIMSK|=1<<PCIE; // General Interrupt Mask Register / PCIE bit activates external interrupts
#asm("sei")
}
// External Interrupt 0 service routine
interrupt [EXT_INT1] void ext_int1_isr(void) {
if((PIND & (1<<CS))== 0){
// If edge is falling, the command and index variables shall be initialized
// and the 4-bit overflow counter of the USI communication shall be activated:
reqID = 0;
index = 0;
USICR |= (1<<USIOIE);
USISR = 1<<USIOIF; // Clear Overflow bit
}
else{
// If edge is rising, turn the 4-bit overflow interrupt off:
USICR &= ~(1<<USIOIE);
}
}
interrupt [USI_OVERFLOW] void usi_ovf_isr(void) {
switch(reqID) {
// Switch-Case to respond according to request from Master:
case 0: // If reqID value is zero (just initialized), then first message is the reqID.
reqID = USIDR; // Read in from USIDR register
USISR = 1<<USIOIF; // Clear Overflow bit
case 'T':
// Write value to send back into USIDR and clear the overflow bit:
USIDR = nG & 0x03;
USISR = 1<<USIOIF;
index++; // Increment index to transmit the folloing element next
break;
case 'H':
// Write value to send back into USIDR and clear the overflow bit:
USIDR = fG1;
USISR = 1<<USIOIF;
index++; // Increment index to transmit the folloing element next
break;
// Write value to send back into USIDR and clear the overflow bit:
case 'B':
USIDR = fG2;
USISR = 1<<USIOIF;
index++; // Increment index to transmit the folloing element next
break;
// Write value to send back into USIDR and clear the overflow bit:
case 'I':
USIDR = fG3;
USISR = 1<<USIOIF;
index++; // Increment index to transmit the folloing element next
break;
default:
// Default option of Switch-Case. Send 'reqID' back for debugging.
USIDR = reqID;
fG=fG1+fG2+fG3;
USISR = 1<<USIOIF;
}
}
//***********************************************timer1************************************************
void Tim1Init(void)
{
#asm("cli")
TCCR1A = (1<<COM1A0); //toggle on compare
TCCR1B = (1<<WGM12)|(1<<CS12)|(1<<CS10); // set timer CTC mode, prescaler 1024
TIMSK = (1<<OCIE1A);
#asm("sei")
}
void SetUpTim1A(unsigned int Foc) //set value OCR1A register
{
unsigned int TimDiv;
unsigned char ClockSelect=0;
unsigned char i;
for(i=0;i<=4;i++) {
TimDiv = (F_CPU/(2*16*Foc*N[i])-1);
if(TimDiv >= 0 && TimDiv<VFG_TIMER_MAX){
ClockSelect=i+1;
break;
}
}
#asm("cli")
OCR1A = TimDiv;
TCCR1B = (1<<WGM12) | (ClockSelect<<CS10);
#asm("sei")
}
void UpdateTim1A(unsigned int freq) //хранение старого значения
{
static unsigned int fG_old = 0;
if (fG_old != freq)
{
SetUpTim1A(freq);
fG_old = freq;
}
}
void set_out_pin (unsigned char num){
nG=1<<num;
VFG_DDR = nG;
}
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
VFG_PORT = (VFG_PORT^nG)&(nG);
}
void main(void)
{
static unsigned int fG_old = 0;
SpiSlaveInit();
Tim1Init();
UpdateTim1A(fG);
#asm("sei")
for(;;) {
if (fG_old != fG) { //проверка, не изменилось ли старое значение
SetUpTim1A(fG);
fG_old = fG;
}
//nG=0; //номер генератора
//fG=773; //частота генератора
set_out_pin (nG);
SetUpTim1A(fG);
}
}
Пн июн 05, 2023 13:33:36
Пн июн 05, 2023 13:53:08
Пн июн 05, 2023 14:16:14
Пн июн 05, 2023 14:40:27
UPD: после того как подставил вычисление fG и nG в основной цикл, МК стал дергать PB0, на изменения в коде мастера не реагирует.
Пн июн 05, 2023 16:12:11
Пн июн 05, 2023 16:36:13
Пн июн 05, 2023 16:56:32
#include <tiny2313.h>
#include <math.h>
#include <io.h>
#define F_CPU (8000000)
#define VFG_TIMER_MAX (65535)
#define VFG_DDR DDRB
#define VFG_PORT PORTB
#define CS PORTD3 // Chip select
#define DO PORTB5 // MISO or Data Out
#define USCK PORTB7 // Clock
typedef union //объединение
{
unsigned long int w ; // w as WORD
unsigned int h[2]; // h as HALF-WORD
unsigned char b[4]; // b as BYTE
} Union32;
Union32 dFi;
unsigned int fG;
unsigned char nG;
unsigned int N[]={1,8,64,256,1024};
unsigned char flag_RT = 0;
unsigned char ch_num = 0;
volatile char reqID = 0; // This is for the first byte we receive, which is intended to be the request identifier
volatile unsigned char index = 0; // this is to send back the right element in the array
//***********************************************USI************************************************
void SpiSlaveInit() {
#asm("cli")
USICR = ((1<<USIWM0)|(1<<USICS1)); // Activate 3- Wire Mode and use of external clock but NOT the interrupt at the Counter overflow (USIOIE)
PORTB |= 1<<CS; // Activate Pull-Up resistor on PB0
PCMSK|=1<<CS; // Active Interrupt on PD3
GIMSK|=1<<PCIE; // General Interrupt Mask Register / PCIE bit activates external interrupts
#asm("sei")
}
// External Interrupt 0 service routine
interrupt [EXT_INT1] void ext_int1_isr(void) {
if((PIND & (1<<CS))== 0){
// If edge is falling, the command and index variables shall be initialized
// and the 4-bit overflow counter of the USI communication shall be activated:
reqID = 0;
index = 0;
flag_RT = 0;
USICR |= (1<<USIOIE);
USISR = 1<<USIOIF; // Clear Overflow bit
}
else{
// If edge is rising, turn the 4-bit overflow interrupt off:
USICR &= ~(1<<USIOIE);
}
}
interrupt [USI_OVERFLOW] void usi_ovf_isr(void) {
switch(reqID) {
case 0:
ch_num = USIDR;
USISR = 1<<USIOIF; // Clear Overflow bit
reqID++;
break;
case 1:
dFi.b[0] = USIDR;
USISR = 1<<USIOIF; // Clear Overflow bit
reqID++;
break;
case 2:
dFi.b[1] = USIDR;
USISR = 1<<USIOIF; // Clear Overflow bit
reqID++;
break;
case 3:
dFi.b[2] = USIDR;
USISR = 1<<USIOIF; // Clear Overflow bit
reqID++;
flag_RT = 1;
break;
}
}
//***********************************************timer1************************************************
void Tim1Init(void)
{
#asm("cli")
TCCR1A = (1<<COM1A0); //toggle on compare
TCCR1B = (1<<WGM12)|(1<<CS12)|(1<<CS10); // set timer CTC mode, prescaler 1024
TIMSK = (1<<OCIE1A);
#asm("sei")
}
void SetUpTim1A(unsigned int Foc) //set value OCR1A register
{
unsigned int TimDiv;
unsigned char ClockSelect=0;
unsigned char i;
for(i=0;i<=4;i++) {
TimDiv = (F_CPU/(2*16*Foc*N[i])-1);
if(TimDiv >= 0 && TimDiv<VFG_TIMER_MAX){
ClockSelect=i+1;
break;
}
}
#asm("cli")
OCR1A = TimDiv;
TCCR1B = (1<<WGM12) | (ClockSelect<<CS10);
#asm("sei")
}
void UpdateTim1A(unsigned int freq) //хранение старого значения
{
static unsigned int fG_old = 0;
if (fG_old != freq)
{
SetUpTim1A(freq);
fG_old = freq;
}
}
void set_out_pin (unsigned char num){
nG=1<<num;
VFG_DDR = nG;
}
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
VFG_PORT = (VFG_PORT^nG)&(nG);
}
void main(void)
{
static unsigned int fG_old = 0;
SpiSlaveInit();
Tim1Init();
UpdateTim1A(fG);
#asm("sei")
for(;;) {
if (fG_old != fG) { //проверка, не изменилось ли старое значение
SetUpTim1A(fG);
fG_old = fG;
}
nG= ch_num; //номер генератора
fG= dFi.b[0]+dFi.b[1]+dFi.b[2]; //частота генератора
if (flag_RT = 1) {
set_out_pin (nG);
SetUpTim1A(fG);
}
}
}