Author Topic: Communication with pc  (Read 280 times)

Amaury

  • Newbie
  • *
  • Posts: 6
Communication with pc
« on: August 02, 2017, 01:27:11 pm »
Hello

 I'm trying to send some data from digispark attiny85 usb to the pc. I'm using the usi has uart (http://www.technoblogy.com/list?VVD), and failing miserably, even setting the ports of usb- and usb+ has tx and rx.
i'm super newbie with digispark so i dont know how to proceed, is there a library so i can send the data to a file or directly to a program that reads the data?
Any exemples or tutorials would help.
thanks

DaveFromRI

  • Newbie
  • *
  • Posts: 6
Re: Communication with pc
« Reply #1 on: August 03, 2017, 06:39:46 am »
I'm a newbie too, and my first project didn't even use serial IO.

However, why not post your code? I'm not afraid to try it on my Digispark Pro and see what happens, but better still, if someone out there sees your code, they may have an "Ah Hah! moment and be able to quickly help you.

Just a thought.

Amaury

  • Newbie
  • *
  • Posts: 6
Re: Communication with pc
« Reply #2 on: August 03, 2017, 04:24:15 pm »
The code is in the liink above but i can still post it here. But regardless the code isnt there a library that allows to send data to a pc or file? The data is located in the flash mem, and then it sends it to the pc.
Thanks for the reply
Code: [Select]
/* ATtiny85  USI UART

   David Johnson-Davies - www.technoblogy.com - 6th May 2015
   and Edgar Bonet - www.edgar-bonet.org
   ATtiny85 @ 16MHz (external crystal; BOD disabled)
   
   CC BY 4.0
   Licensed under a Creative Commons Attribution 4.0 International license:
   http://creativecommons.org/licenses/by/4.0/
*/

// Constant
const int DataIn = PINA6;         // USI DI

// USI UART **********************************************

unsigned char ReverseByte (unsigned char x) {
  x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
  x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
  x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
  return x;   
}

// Initialise USI for UART reception.
void InitialiseUSI (void) { 
  DDRA &= ~(1<<DataIn);           // Define DI as input
  USICR = 0;                      // Disable USI.
  GIFR = 1<<PCIF0;                // Clear pin change interrupt flag.
  GIMSK |= 1<<PCIE0;              // Enable pin change interrupts
  PCMSK0 |= 1<<PCINT6;            // Enable pin change on pin 6
}

// Pin change interrupt detects start of UART reception.
ISR (PCINT0_vect) {
  if (PINA & 1<<DataIn) return;   // Ignore if DI is high
  GIMSK &= ~(1<<PCIE0);           // Disable pin change interrupts
  TCCR0A = 2<<WGM00;              // Timer in CTC mode
  TCCR0B = 2<<CS00;               // Set prescaler to /8
  OCR0A = 103;                    // Shift every (103+1)*8 cycles
  TCNT0 = 206;                    // Start counting from (256-52+2)
  // Enable USI OVF interrupt, and select Timer0 compare match as USI Clock source:
  USICR = 1<<USIOIE | 0<<USIWM0 | 1<<USICS0;
  USISR = 1<<USIOIF | 8;          // Clear USI OVF flag, and set counter
}

// USI overflow interrupt indicates we've received a byte
ISR (USI_OVF_vect) {
  USICR = 0;                      // Disable USI         
  int temp = USIDR;
  Display(ReverseByte(temp));
  GIFR = 1<<PCIF0;                // Clear pin change interrupt flag.
  GIMSK |= 1<<PCIE0;              // Enable pin change interrupts again
}

void Display (char c) {
  // Process received byte here
}

// Main **********************************************

void setup (void) {
  InitialiseUSI();
}

void loop (void) {
}