Skip to main content

Digital

This source code creates a array for controlling all digital pins at one place in form Pins array which would be used so that we get meaningful functions to work upon and also the implementation of rustduino library is easier for the user. For more details see section 16,17,25 and 26 of ATMEGA2560P datasheet.

/// Include the required source codes before struct creation
use crate::hal::pin::*;
use core::ptr::{read_volatile, write_volatile};

Structure#

This struct contain digital pin and its corresponding digital pin no.

pub struct DigitalPin {
pub pin: Pin,
pub pinno: u32,
}

Implementations for DigitalPin#

impl toggle#

Toggles the appropriate bit in PINxn register so that the mode of the pin is changed from high to low or vice versa.

pub fn toggle(&mut self) {
unsafe { write_volatile(&mut (*self.pin.port).pin, 0x1 << self.pin.pin) }
}

Usage#

use rustduino::atmega2560p::hal::pin::Pins;
let mut pins = Pins::new();// This pins represents pin 7 of port B ( pin 13).
pins.digital[13].toggle();

impl high#

Set the pin to high output value.

pub fn high(&mut self) { /* fields omitted */}

Usage#

pins.digital[13].high(); //This sets pin high.

impl low#

Sets the pin to low output value.

pub fn low(&mut self) {/* fields omitted */}

Usage#

pins.digital[13].low(); //This sets pin low.

impl write (used for Analog Write)#

We have to output or write a PWM wave to a digital pin, which can be 2 to 13 or 44 to 46 other pins will not work. Of these pins, 4 and 13 give output at 980 hertz while all pin except 4 and 13 are set to give output at 490 hertz.

The analog Write function takes in the value generated by mapping the 10 bit result from analog Read to a 8 bit number.

pub fn write(&mut self, value1: u8) {/* fields omitted */}

Usage#

let mut pins = Pins::new();
let b: u8 = {/* Make the input value ready to be sent through a digital pin.*/};
pins.digital[13].write(b); // Give output from the 13th digital pin.
Last updated on by Devansh